What do double parentheses mean in a function call? e.g. func(stuff)(stuff)?

Original title:

"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 (sys.stdout) is doing. It sort of reminds me of a Javascript closure or something. But I don't know how to look up this idiom in the Python docs.

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.

Example:

def returnFunction():
    def myFunction():
        print('hello!')
    return myFunction

Demo:

>>> returnFunction()()
hello!

You could have alternatively done:

>>> result = returnFunction()
>>> result()
hello!

Visualization:

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 .

Example

1- funcWrapper

def funcwrapper(y):
    def abc(x):
        return x * y + 1
    return abc

result = funcwrapper(3)(5)
print(result)

2- funcWrapper

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.

Python parentheses primer — Reuven Lerner, Functions are the bread and butter of JavaScript programming. In the following example, makeNoise does not list any parameter names, whereas This provides some isolation between functions—each function call acts in its own little A function value can do all the things that other values can do—you can use it in� Functions are so-called first-class objects or first-class citizens in Python, which is a fancy way of saying they’re values just like strings or numbers. This way, you can assign a function to a variable, pass it to another function, or even return one from another. print() isn’t different in this regard. For instance, you can take

Functions :: Eloquent JavaScript, The actual parameters (arguments) to a function call are introduced in the class MyClass: """ A simple example class """ i = 12345 def f(self): return Python simply takes this idea one step further and gives meaning to the indentation. a virtual env; you can try doing one of the things mentioned here. 3. void main identifies the name of the function main. The void identifies the type of function or what the function produces. In the case of main, it doesn’t produce anything, and the C term for that is “void.” 4. Two empty parentheses follow the function name. Sometimes, there may be items in these parentheses. 5.

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.

Comments
  • 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.