What does it mean to “call” a function in Python?

What does “challenge” mean? How would you “call” a function in Python?

+11
source share
4 answers

When you call a function, you basically just tell the program to execute that function. Therefore, if you have a function that added two numbers, for example:

def add(a,b): return a + b 

you call the function as follows:

 add(3,5) 

which will return 8. In this case, you can put any two numbers in parentheses. You can also call this function:

 answer = add(4,7) 

In this case, the value of the variable will be 11.

+19
source

I will give a slightly expanded answer. In Python, functions are first-class objects . This means that they can be "dynamically created, destroyed, transferred to functions, returned as values, and have all rights, like other variables in the programming language."

Calling an instance of a function / class in Python means calling the __call__ method of this object. For old-style classes, instance instances are also called, but only if the object that creates them has the __call__ method. The same applies to new-style classes, with the exception of the term "instance" with new-style classes. Rather, these are "types" and "objects."

As stated in the Python 2 data model page , for function objects, class instances (old-style classes) and class objects (new -style classes), " x(arg1, arg2, ...) is short for x.__call__(arg1, arg2, ...) ".

Thus, whenever you define a function with the shorthand def funcname(parameters): you really just create the object using the __call__ method, and the shorthand for __call__ is just the instance name and follow it with parentheses containing the call arguments. Since functions are first-class objects in Python, you can create them on the fly with dynamic parameters (and therefore accept dynamic arguments). This will come in handy for the functions / classes of decorators, which you will read about later.

I currently suggest reading the Official Python Guide .

+12
source

To “call” means to make a link in the code to a function that is written elsewhere. This "calling" function can be executed in the standard Python library (material that is installed with Python), third-party libraries (material written by other people that you want to use), or your own code (material that you wrote). For instance:

 #!/usr/env python import os def foo(): return "hello world" print os.getlogin() print foo() 

I created a function called "foo" and , called , later with this print statement. I imported the Python standard os library, then I called the getlogin function in that library.

+2
source

when you call a function, it is called a "call" to the function. For example, suppose you define a function that finds the average of two numbers like this -

 def avgg(a,b) : return (a+b)/2; 

Now, to call a function, you like it.

 x=avgg(4,6) print x 

the value of x will be 5.

0
source

Source: https://habr.com/ru/post/955078/


All Articles