2
def x():
    print("This is a function x")

I defined a function named x and assigned it to y

y = x
y() # Calling this would call the original function

But what is happening internally, is it assigning a pointer or reference to the function or an instance-like thing I could not understand? Or is it giving a link to the memory address of where x is defined?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
YouKnowWhoIAm
  • 334
  • 1
  • 12
  • A function is created and bound to a name (`x`), then the name `y` is bound to the same thing as `x` (in this case, your function). That function object will exist until there are no references to it, at which point it will be garbage collected. – jedwards Jun 16 '18 at 06:09
  • 3
    Yes, internally `y` is a name bound to the same pointer that the name `x` is bound to. But it's best not to think about pointers in Python. Instead, try to embrace the Python data model on its own terms. You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Jun 16 '18 at 06:12
  • You might be interested in the `dis` module. Notably, this does something quite different in global scope (where variables are names from the current `globals` dict) vs in function scope (where variables are simply indices into the stack frame). And I *think* there are still cases where a fallback is used ... – o11c Jun 16 '18 at 07:29
  • 1
    @PM2Ring In the course video, i have the function x defined and y is assigned to x later the x is deleted by del x and it is called upon by x() then the interpreter says x is not defined then the function y is called by y() but it is executed. So is it possible to remove the reference of the function x, if yes then how can y call the function if the x is deleted or is it just an object that is deleted not the program that is stored in the memory – YouKnowWhoIAm Jun 16 '18 at 07:38
  • @PM2Ring and also if the function x is class function so if i now assign it to y can the y handle the data that is held by x or will it be a fresh copy of it – YouKnowWhoIAm Jun 16 '18 at 07:41
  • 1
    See https://stackoverflow.com/q/21053380/3001761 – jonrsharpe Jun 16 '18 at 08:10
  • Jon Sharpe's link answers your question about `del`. To understand what happens when you call a method on a class instance, you need to learn about [descriptors](https://docs.python.org/3/howto/descriptor.html), the Functions and Methods section of that article describes what happens when you access a method. This is a fairly advanced topic, so don't expect to understand that article after a single reading. ;) – PM 2Ring Jun 16 '18 at 08:27

4 Answers4

2

Functions are objects just like everything else in Python. Assigning a function to a name is just like assigning a string, or a list - as with those, a new reference is created to the object.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • So if i add a reference to that and then delete the original function name then will the second reference live up to the function – YouKnowWhoIAm Jun 27 '18 at 12:46
  • 1
    Yes because *both* names are just references. The original name is not any different; deleting it doesn't delete the function, it just removes that reference to it. Python will delete objects internally when there are no more references to them. – Daniel Roseman Jun 27 '18 at 12:52
1

Since in python everything is object(even the functions are first class objects) so, when you define a function 'x' and assign it to variable(name) 'y' which is nothing but a binding of a name to an object 'x'. For this statement,

y=x()

the name y is bound to the scope of function x. So,

y() 

is bound to the function call for x.

NightOwl19
  • 419
  • 5
  • 24
0

By assigning a function to a variable:

y = x

the reference of object of x() is stored in variable y

when you call the assigned variable as a function by:

y()

this calls the function x by using the stored reference

FutureJJ
  • 2,368
  • 1
  • 19
  • 30
  • 1
    So any variable that has the reference of the function x can actually do what x can do like if i have the function def x(self,name): self.name = name and when i assign y = x can it have the data stored in x too? – YouKnowWhoIAm Jun 16 '18 at 07:21
  • Yes, any variable having the reference do what x does @YouKnowWhoIAm , because you are storing the reference of function x in a variable so calling the variable as a function, calls x() through its reference. – FutureJJ Jun 16 '18 at 07:24
  • @YouKnowWhoIAm If x is a function having two input parameters ie: x(var_a, var_b) and y = x, then y if called as function accepts two input parameters just like x – FutureJJ Jun 16 '18 at 07:34
  • 1
    Also will the data held by function is also copied into the y or is it a fresh copy of the function – YouKnowWhoIAm Jun 16 '18 at 07:44
  • @YouKnowWhoIAm, can you please clarify: "data held by a function". – FutureJJ Jun 17 '18 at 13:35
  • I mean if x is some function in a class then will y get the data of x of some instance, like x is in a particular instance and has some value length = 20, so if i refer it by y and later delete x then is my data lost or is it copied into y – YouKnowWhoIAm Jun 18 '18 at 18:06
0

Like in (nearly) every other programming language, functions are objects.

def x(): 
    print("This is a function x")

x is an object of type function now

y = x 
y() # Calling this would call the original

So in the y-variable a pointer to the x-object/function is stored. If you call y(), y will look up its 'origin' and calls x...

Because all functions are objects you can also pass functions as parameters:

def test(func):
    func()

test(x)

would output:

This is a function x

Hope I could help!

Fipsi
  • 670
  • 1
  • 9
  • 21
  • 1
    In the course video, i have the function x defined and y is assigned to x later the x is deleted by del x and it is called upon by x() then the interpreter says x is not defined then the function y is called by y() but it is executed. So is it possible to remove the reference of the function x, if yes then how can y call the function if the x is deleted or is it just an object that is deleted not the program that is stored in the memory – YouKnowWhoIAm Jun 16 '18 at 07:37
  • If you want to remove the y reference to the x function you can just say y = 0 or something similar.... And you can't just delete a function... You can only make a reference to a non existing function... Like saying y=x before defining x.... Hope I could help! – Fipsi Jun 16 '18 at 08:08
  • But in the tutorial the person deleted the function, i know a function cant be deleted but i thought may be in python we can do it. Someone also said yes and said x is just a reference so yes we can delete x. – YouKnowWhoIAm Jun 18 '18 at 18:11