keyword-argument

forcing keyword arguments in python 2.7

为君一笑 提交于 2019-12-10 10:11:48
问题 I know I can use * to force all keyword arguments to a function/method to be "named". If I have def abc(a, *, x=10, z=30): pass then the following all work abc(5) abc(8, x=12) abc(9, z=31) abc(x=17, a=4) even if I change the function signature to def abc(a, *, x=10, y=20, z=30) , and abc(7, 13) throws an error. This is extremely important because, I can use the logical place, which will help maintenance over time, without being forced to use the end position based on history. But * is not

named parameters with default values in groovy

a 夏天 提交于 2019-12-09 14:04:11
问题 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=

Clojure applying a map and keyword arguments destruction

坚强是说给别人听的谎言 提交于 2019-12-09 08:33:48
问题 Consider a function with the following signature: (defn make-widget [& {:keys [x y] :or {x 10 y 20}}] ...) What is the best way to pass a map to the function, e.g.: (make-widget {:x 100}) or (make-widget {:y 200 :x 0}) What I have currently thought of is via vec , flatten and apply e.g.: (apply make-widget (flatten (vec ({:x 100})) I strongly believe there is a better way to do this. Can you please consider one? 回答1: I can't think of a more elegant way, either, though it seems to me to that

How to best *fake* keyword style function arguments in Rust?

喜你入骨 提交于 2019-12-08 19:36:15
问题 I'm interested to have something functionally similar to keyword arguments in Rust, where they're currently not supported. For languages that provide keyword argument, something like this is common: panel.button(label="Some Button") panel.button(label="Test", align=Center, icon=CIRCLE) I've seen this handled using the builder-pattern, eg: ui::Button::new().label("Some Button").build(panel) ui::Button::new().label("Test").align(Center).icon(CIRCLE).build(panel) Which is fine but at times a

Can you have required keyword arguments in Javascript or Python?

江枫思渺然 提交于 2019-12-07 16:28:31
问题 Can you have required keyword arguments in javascript or python? Is this a common feature of programming languages, or is it new and rare? They would be analogous to this implementation of keyword arguments in Ruby in Ruby 2.1+ def obvious_total(subtotal:, tax:, discount:) subtotal + tax - discount end obvious_total(subtotal: 100, tax: 10, discount: 5) # => 105 (The above example comes directly from https://robots.thoughtbot.com/ruby-2-keyword-arguments) I'd like to know because I was really

Separating **kwargs for different functions

帅比萌擦擦* 提交于 2019-12-06 19:34:51
问题 Given a higher order function that takes multiple functions as arguments, how could that function pass key word arguments to the function arguments? example def eat(food='eggs', how_much=1): print(food * how_much) def parrot_is(state='dead'): print("This parrot is %s." % state) def skit(*lines, **kwargs): for line in lines: line(**kwargs) skit(eat, parrot_is) # eggs \n This parrot is dead. skit(eat, parrot_is, food='spam', how_much=50, state='an ex-parrot') # error state is not a keyword arg

forcing keyword arguments in python 2.7

喜你入骨 提交于 2019-12-06 03:22:09
I know I can use * to force all keyword arguments to a function/method to be "named". If I have def abc(a, *, x=10, z=30): pass then the following all work abc(5) abc(8, x=12) abc(9, z=31) abc(x=17, a=4) even if I change the function signature to def abc(a, *, x=10, y=20, z=30) , and abc(7, 13) throws an error. This is extremely important because, I can use the logical place, which will help maintenance over time, without being forced to use the end position based on history. But * is not valid in Python 2.7, and abc(a, *args, x=10, z=30) (which I tried) doesn't work either. Is there a way to

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 02:52:16
问题 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__

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

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:59:40
问题 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

Python file keyword argument?

邮差的信 提交于 2019-12-05 02:31:59
In command line I am able to pass arguments to a python file as: python script.py arg1 arg2 I can than retrieve arg1 and arg2 within script.py as: import sys arg1 = sys.argv[1] arg2 = sys.argv[2] However, I would like to send keyword arguments to a python script, and retrieve them as a dictionary: python script.py key1=value1 key2=value2 Then I would like to access the keyword arguments as a dictionary within python: {'key1' : 'value1', 'key2' : 'value2'} Is this possible? I think what you're looking for is the argparse module https://docs.python.org/dev/library/argparse.html . It will allows