keyword-argument

How do I add keyword arguments to a derived class's constructor in Python?

淺唱寂寞╮ 提交于 2019-12-04 07:23:42
I want to add keyword arguments to a derived class, but can't figure out how to go about it. Trying the obvious class ClassA(some.package.Class): def __init__(self, *args, **kwargs): super(ClassA, self).__init__(*args, **kwargs) class ClassB(ClassA): def __init__(self, *args, a='A', b='B', c='C', **kwargs): super(ClassB, self).__init__(*args, **kwargs) self.a=a self.b=b self.c=c fails because I can't list parameters like that for ClassB 's __init__ . And class ClassB(ClassA): def __init__(self, *args, **kwargs): super(ClassA, self).__init__(*args, **kwargs) self.a=a self.b=b self.c=c of course

Can I use a dynamic mapping to unpack keyword arguments in Python?

本秂侑毒 提交于 2019-12-04 05:34:17
Long story short, I want to call format with arbitrarily named arguments, which will preform a lookup. '{Thing1} and {other_thing}'.format(**my_mapping) I've tried implementing my_mapping like this: class Mapping(object): def __getitem__(self, key): return 'Proxied: %s' % key my_mapping = Mapping() Which works as expected when calling my_mapping['anything'] . But when passed to format() as shown above I get: TypeError: format() argument after ** must be a mapping, not Mapping I tried subclassing dict instead of object , but now calling format() as shown raises KeyError . I even implemented _

named parameters with default values in groovy

浪尽此生 提交于 2019-12-03 22:16:39
Is it possible to have named parameters with default values in groovy? My plan is to make a sort of object factory, which can be called with no arguments at all in order to get an object with default values. Also, I'd need the functionality to explicitly set any of the params for the object. I believe this is possible with Python keyword arguments, for example. The code I'm attempting with right now is something like below // Factory method def createFoo( name='John Doe', age=51, address='High Street 11') { return new Foo( name, age, address ) } // Calls Foo foo1 = createFoo() // Create Foo

Setting the default value of a function input to equal another input in Python

喜你入骨 提交于 2019-12-03 11:04:07
问题 Consider the following function, which does not work in Python, but I will use to explain what I need to do. def exampleFunction(a, b, c = a): ...function body... That is I want to assign to variable c the same value that variable a would take, unless an alternative value is specified. The above code does not work in python. Is there a way to do this? Thank you. 回答1: def example(a, b, c=None): if c is None: c = a ... The default value for the keyword argument can't be a variable (if it is, it

empty dictionary as default value for keyword argument in python function: dictionary seems to not be initialised to {} on subsequent calls? [duplicate]

那年仲夏 提交于 2019-12-03 10:47:37
问题 This question already has an answer here : numpy array subclass unexpedly shares attributes across instances (1 answer) Closed 4 years ago . Here's a function. My intent is to use keyword argument defaults to make the dictionary an empty dictionary if it is not supplied. >>> def f( i, d={}, x=3 ) : ... d[i] = i*i ... x += i ... return x, d ... >>> f( 2 ) (5, {2: 4}) But when I next call f, I get: >>> f(3) (6, {2: 4, 3: 9}) It looks like the keyword argument d at the second call does not point

empty dictionary as default value for keyword argument in python function: dictionary seems to not be initialised to {} on subsequent calls? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-03 01:14:15
This question already has an answer here : numpy array subclass unexpedly shares attributes across instances (1 answer) Here's a function. My intent is to use keyword argument defaults to make the dictionary an empty dictionary if it is not supplied. >>> def f( i, d={}, x=3 ) : ... d[i] = i*i ... x += i ... return x, d ... >>> f( 2 ) (5, {2: 4}) But when I next call f, I get: >>> f(3) (6, {2: 4, 3: 9}) It looks like the keyword argument d at the second call does not point to an empty dictionary, but rather to the dictionary as it was left at the end of the preceding call. The number x is reset

Understanding Ruby method parameters syntax

你离开我真会死。 提交于 2019-12-02 11:17:24
问题 I've been following an RSpec tutorial on Pluralsight for creating a basic card game. When the class is defined as such: class Card def initialize(suit:, rank:) @suit = suit @rank = case rank when :jack then 11 when :queen then 12 when :king then 13 else rank end end end the RSpec test code is for example: RSpec.describe 'a playing card' do it 'has a suit' do raise unless Card.new(suit: :spades, rank: 4).suit == :spades end end I haven't encountered method parameter syntax like this (suit:

Understanding Ruby method parameters syntax

梦想与她 提交于 2019-12-02 03:06:50
I've been following an RSpec tutorial on Pluralsight for creating a basic card game. When the class is defined as such: class Card def initialize(suit:, rank:) @suit = suit @rank = case rank when :jack then 11 when :queen then 12 when :king then 13 else rank end end end the RSpec test code is for example: RSpec.describe 'a playing card' do it 'has a suit' do raise unless Card.new(suit: :spades, rank: 4).suit == :spades end end I haven't encountered method parameter syntax like this (suit: :spades, rank: 4) . Can someone explain what this means, or point me in the right direction on where to

When to use keyword arguments aka named parameters in Ruby

那年仲夏 提交于 2019-12-01 02:28:27
Ruby 2.0.0 supports keyword arguments (KA) and I wonder what the benefits/use-cases are of this feature in context of pure Ruby, especially when seen in light of the performance penalty due to the keyword matching that needs to be done every time a method with keyword arguments is called. require 'benchmark' def foo(a:1,b:2,c:3) [a,b,c] end def bar(a,b,c) [a,b,c] end number = 1000000 Benchmark.bm(4) do |bm| bm.report("foo") { number.times { foo(a:7,b:8,c:9) } } bm.report("bar") { number.times { bar(7,8,9) } } end # user system total real # foo 2.797000 0.032000 2.829000 ( 2.906362) # bar 0

understanding '*' “keyword only” argument notation in python3 functions [duplicate]

天涯浪子 提交于 2019-11-30 08:44:03
问题 This question already has answers here : Bare asterisk in function arguments? (6 answers) Closed 3 months ago . I am having some difficulty behaviour of keyword only arguments feature in python3 when used with partial. Other info on keyword only arguments. Here is my code: def awesome_function(a = 0, b = 0, *, prefix): print('a ->', a) print('b ->', b) print('prefix ->', prefix) return prefix + str(a+b) Here is my understanding of partial: >>> two_pow = partial(pow, 2) >>> two_pow(5) 32 >>>