What do double parentheses mean in a function call? e.g. func(stuff)(stuff)?
"Help me understand this weird Python idiom?
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)"
I use this idiom all the time to print a bunch of content to standard out in utf-8 in Python 2.*:
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
But to be honest, I have no idea what the
Can any of you fine folks explain what's going on here? Thanks!
.getwriter returns a functioncallable object; you are merely calling it in the same line.
def returnFunction(): def myFunction(): print('hello!') return myFunction
>>> returnFunction()() hello!
You could have alternatively done:
>>> result = returnFunction() >>> result() hello!
evaluation step 0: returnSomeFunction()() evaluation step 1: |<-somefunction>-->|() evaluation step 2: |<----result-------->|
What do double parentheses mean in a function call? e.g. func(stuff , In either case, the parentheses are required. The function definition must first be entered into the Python shell before it can be Function calls involve an implicit assignment of the argument to the in a function, allowing you to take advantage of all the things functions are good for. def double(thing): return 2 * thing . Functions are usually characterized by the name of the function followed by parentheses: function_name (). Functions require inputs or objects to do stuff to. They may also have options that modify the actions that are carried out on the objects. Both the objects/data and the options are called arguments, and there are these two different types.
codecs.getwriter('utf-8') returns a class with
StreamWriter behaviour and whose objects can be initialized with a stream.
>>> codecs.getwriter('utf-8') <class encodings.utf_8.StreamWriter at 0x1004b28f0>
Thus, you are doing something similar to:
sys.stdout = StreamWriter(sys.stdout)
5. Functions — Beginning Python Programming for Aspiring Web , v[[i]] double brackets for indexing ( Part[v, i] ) Mathematica also knows about a whole bunch of other functions, such as trigonometric functions. (For example, sin(1.2) means the variable named sin multiplied by 1.2 .) Round parentheses are used to mean two completely different things in traditional� Parentheses. Parentheses (always used in pairs) allow a writer to provide additional information. The parenthetical material might be a single word, a fragment, or multiple complete sentences. Whatever the material inside the parentheses, it must not be grammatically integral to the surrounding sentence. If it is, the sentence must be recast.
Calling the wrapper function with the double parentheses of python flexibility .
def funcwrapper(y): def abc(x): return x * y + 1 return abc result = funcwrapper(3)(5) print(result)
def xyz(z): return z + 1 def funcwrapper(y): def abc(x): return x * y + 1 return abc result = funcwrapper(3)(xyz(4)) print(result)
Why did Mathematica choose brackets for function arguments over , It's thus super easy to take things for granted when you're an expert. Perhaps the most obvious use for parentheses in Python is for calling functions and run this code, you can an error message that is true, but whose meaning isn't Notice the double parentheses here; the outer ones are for the call to� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.
Best Python Code Examples, A function can access an outer variable as well, for example: Parameters. We can pass arbitrary data to functions using parameters (also called function arguments) . Or at least put the opening parentheses there as follows: There must be an agreement within the team on the meaning of the prefixes. Brackets are used to add information or a comment, but the different types are not interchangeable. Learn how to use brackets with clear examples.
- However, in this example the second set of parantheses is a class instantiation operator, not a function call. (See docs.python.org/tutorial/classes.html#class-objects) Also, please do not edit the question to match your answer.
- @junjanes: Thank you for pointing out that detail (though a "class instantiation operator" is in fact just class (callable object) or factory function (callable object)). You misinterpret though the rationale for editing: it is not to make my answer "match better", but rather to better summarize the question. (Incidentally, it does not in fact make my answer match better, anymore than it makes your answer match better.) In light of your concern though, I will re-add the original title in the question.