keyword-argument

python not accept keyword arguments

孤人 提交于 2020-01-11 05:17:06
问题 I am trying to make my code NOT to accept keyword arguments just like some bulitins also do not accept keyword arguments, but, I am unable to do so. Here, is my thinking according to my limited understanding:- def somefunc(a,b): print a,b somefunc(10,20) Output: 10 20 Now, When I run the following (I know this is not expected to be keyword argument in the function definition, but, looking at the function call, it seems to be the same syntax as that of when calling a function which accepts

Odd method behaviour with optional first hash parameter and keyword_args

点点圈 提交于 2020-01-02 05:40:41
问题 I have the following method: def test(first_param = nil, keyword_arg: nil) puts "first_param: #{first_param}" puts "keyword_arg: #{keyword_arg}" end All the following calls do what I expect them to do: test(:something) #=> first_param: something # keyword_arg: test(nil, keyword_arg: :keyword_arg) #=> first_param: # keyword_arg: keyword_arg test({ first_param: :is_a_hash }, keyword_arg: :is_still_working) #=> first_param: {:first_param=>:is_a_hash} # keyword_arg: is_still_working But omitting

Function to set properties of an object of a class composition

别等时光非礼了梦想. 提交于 2019-12-24 11:30:03
问题 I would like construct a class composition that includes a function set_props for setting the instance variables of components. The application for this is in defining new objects for drawing in matplotlib. One example is that I would like to have a function drawMyArrow that draws an arrow with possibly different colors (and other specifications) for its head, tail, and arc. I would like to be able to pass various specifications for the head, tail, and arc via keyword arguments in drawMyArrow

Python optional, positional and keyword arguments

旧街凉风 提交于 2019-12-24 07:38:00
问题 This is a class I have: class metadict(dict): def __init__(self, do_something=False, *args, **kwargs) if do_something: pass super(metadict,self).__init__(*args,**kwargs) The idea is to encapsulate a dictionary and add some functionality with a special keyword. The dictionary can still hold do_something though you can't add it at creation time. For all other aspects it behaves just like a normal dictionary. Anyway, the problem is that whatever I give to args it starts by assigning the first

Optional function arguments with no default value possible?

大兔子大兔子 提交于 2019-12-24 06:49:05
问题 In Chapel, we can set the default value of function formal arguments easily, for example, proc test( a = 1, b = 2.0, c = "hi" ) { ... } and call the function by using keywords also: test( 10 ); // a = 10, b = 2.0, c = "hi" test( b = 3.14 ); // a = 1, b = 3.14, c = "hi" test( c = "yo" ); // a = 1, b = 2.0, c = "yo" Here, I am wondering if it is possible to define a keyword argument that does not require a predefined default value. More specifically, I would like to write a function that can

Python file keyword argument?

好久不见. 提交于 2019-12-22 03:54:30
问题 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? 回答1: I think what

When to use keyword arguments aka named parameters in Ruby

痞子三分冷 提交于 2019-12-19 05:08:51
问题 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

What is the purpose of bare asterix in function arguments?

夙愿已清 提交于 2019-12-17 16:43:19
问题 I've seen this SO question (this is not a duplicate): Python bare asterisk in function argument In python-3.x you can add a bare * to the function arguments, this means that (quote from docs): Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments. Ok, so, I've defined a function: >>> def f(a, b, *, c=1, d=2, e=3): ... print('Hello, world!') ... I can pass c , d and e variable values only by specifying keywords: >>> f(1, 2, 10, 20, 30)

Initialising in Python using Keyword Args without sharing between instances

£可爱£侵袭症+ 提交于 2019-12-14 02:38:27
问题 How can I use keyword args in an initialiser without sharing it between instances of a class? Example of the bad behaviour below, if I were to add anything to the set foo then it would be added in both instances. In [1]: class Klass: def __init__(self, foo=set()): self.foo = foo In [2]: a = Klass() In [3]: b = Klass() In [4]: a.foo is b.foo Out[4]: True 回答1: Note that this issue will only occur with mutable default arguments - see "Least Astonishment" and the Mutable Default Argument. To use

Python library functions taking no keyword arguments

你。 提交于 2019-12-10 13:54:55
问题 This problem originated when I tried to apply a more functional approach to problems in python. What I tried to do is simply square a list of numbers, no biggie. from operator import pow from functools import partial squared = list(map(partial(pow, b=2), range(10)) As it turns out, this didn't work. TypeError: pow() takes no keyword arguments Confused I checked if pow(b=2, a=3) did. It didn't. I've checked the operator source code, nothing suspicious. Confused, I've begun to doubt my own