arguments

The `arguments` object changes if parameters change

二次信任 提交于 2020-01-14 01:38:55
问题 I just discovered that the arguments object actually changes if one of the parameters change. For example: function some(a, b, c ){ console.log(arguments); args = [ a, b, c ]; a = new Date(); console.log(arguments); console.log(args); } some(1,2,3 ); You will see that while args stays the same (expected behaviour), arguments actually change. Questions: Is this something that is well documented? If so, where? Is there anything else I need to be careful about the arguments object? 回答1: This is

Python subclassing process with parameter

二次信任 提交于 2020-01-13 12:15:12
问题 I'm trying to create an object but as a new process. I'm following this guide and came up with this code. import multiprocessing as mp import time class My_class(mp.Process): def run(self): print self.name, "created" time.sleep(10) print self.name, "exiting" self.x() def x(self): print self.name, "X" if __name__ == '__main__': print 'main started' p1=My_class() p2=My_class() p1.start() p2.start() print 'main exited' But here I'm unable to pass arguments to the object. I searched but found

Pythonic way to pass around many arguments

左心房为你撑大大i 提交于 2020-01-13 10:17:09
问题 I'm working on a package that contains subpackages and several modules within each subpackage. Most of the functions need several arguments (~10) that are initiated in the "main" function. Usually, all the arguments are passed to methods that are called from within a function. What is the best way to pass around these parameters ? Consider the following scenario: def func(arg1, arg2, ...., argn): do something ext_method(arg1, arg3,...argk) # Note: all the arguments are not passed to ext

Python Default Arguments Evaluation [duplicate]

百般思念 提交于 2020-01-13 06:30:09
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (32 answers) Closed 4 years ago . I was reading the python tutorial from Python Documentation Release 2.7.10 and I came across something like this. Code def fun1(a,L=[]): L.append(a) return L print fun1(1) print fun1(2) print fun1(3) def fun2(a,L = None): if L is None: L=[] L.append(a) return L print fun2(1) print fun2(2) print fun2(3) Output [1] [1, 2] [1, 2, 3] [1] [2] [3] Process finished with

Python Default Arguments Evaluation [duplicate]

…衆ロ難τιáo~ 提交于 2020-01-13 06:30:08
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (32 answers) Closed 4 years ago . I was reading the python tutorial from Python Documentation Release 2.7.10 and I came across something like this. Code def fun1(a,L=[]): L.append(a) return L print fun1(1) print fun1(2) print fun1(3) def fun2(a,L = None): if L is None: L=[] L.append(a) return L print fun2(1) print fun2(2) print fun2(3) Output [1] [1, 2] [1, 2, 3] [1] [2] [3] Process finished with

How can I pass keyword arguments as parameters to a function?

梦想与她 提交于 2020-01-13 04:32:27
问题 Say I have a function defined thus: def inner_func(spam, eggs): # code I then want to call a function like this: outer_func(spam=45, eggs="blah") Inside outer_func I want to be able to call inner_func with exactly the same parameters that were passed into outer_func . This can be achieved by writing outer_func like this: def outer_func(spam, eggs): inner_func(spam, eggs) However, I'd like to be able to change the arguments inner_func takes, and change the parameters I pass to outer_func

Introspecting arguments from the constructor function __init__ in Python

做~自己de王妃 提交于 2020-01-12 23:43:50
问题 What is a way to extract arguments from __init__ without creating new instance. The code example: class Super: def __init__(self, name): self.name = name I am looking something like Super.__dict__.keys() type solution. Just to retrieve name argument information without adding any values. Is there such an option to do that? 回答1: You can use inspect >>> import inspect >>> inspect.getargspec(Super.__init__) ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=None) >>> Edit:

Pass dict with non string keywords to function in kwargs

旧时模样 提交于 2020-01-12 07:41:12
问题 I work with library that has function with signature f(*args, **kwargs) . I need to pass python dict in kwargs argument, but dict contains not strings in keywords f(**{1: 2, 3: 4}) Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: f() keywords must be strings How can I get around this without editing the function? 回答1: Non-string keyword arguments are simply not allowed, so there is no general solution to this problem. Your specific example can be fixed by

Modifying a parameter pass to a script (Bash)

流过昼夜 提交于 2020-01-11 05:02:52
问题 I have been looking on Google for quite a while now and can't find anything that is matching what I need/want to do. My objective is to write a script that takes two arguments. It will search through the first argument (which is a list) and detect if the second argument is already in it. For example: list = /bin/foo:/bin/random:random to add to list: /bin/foobar Calling the script will produce the result of /bin/foo:/bin/random:random:/bin/foobar. If the part to add to the list is already in

How can I pass a set of an unknown number of arguments to a function in MATLAB? [duplicate]

百般思念 提交于 2020-01-10 19:57:28
问题 This question already has answers here : uncertain number of inputs for my function (2 answers) Closed 3 years ago . When you have a function that takes a variable amount of arguments (like ndgrid), how can you pass an arbitrary list of arguments to that function? For example I want to make it so that sometimes I pass two vectors to ndgrid and get out two matrices, i.e., [X1,X2] = ndgrid(x1,x2); But other times I might have more X's, so I'll want [X1,X2,X3,X4] = ndgrid(x1,x2,x3,x4) Is there