When you first import a .py file Python processes it to create a .pyc file. Then on subsequent imports Python will import the .pyc file as that's quicker.
If you then change the .py file Python will spot the change and reprocess it to create a new .pyc file.
@Aladdin The command nonlocal x inside f2() specifies that the variable x is the same variable as defined in f1(). It's kind of like global, except that it only works inside the parent function i.e. inside f1().
When you call a function with a parameter, a new reference is created that refers to the object passed in. This is separate from the reference that was used in the function call, so there's no way to update that reference and make it refer to a new object. In your example:
def __init__(self):
self.variable = 'Original'
self.Change(self.variable)
def Change(self, var):
var = 'Changed'
self.variable is a reference to the string object 'Original'. When you call Change you create a second reference var to the object. Inside the function you reassign the reference var to a differen…
can u exlain what this means..i am afraid i am still having doubts with these
When the function foo executes the line return foo1 this causes Python to create an object, put the function foo1 into that object and then return a reference to that object.
When the function foo executes the line return foo1 this causes Python to create an object, put the function foo1 into that object and then return a reference to that object.
Python stores the value 1 in some bit of memory. Suppose it stores it in byte number 1234. That is, the memory is just an array of bytes starting at 0 and 1234 is the offset in the memory where Python stores the data 1.
The reference to this memory is just its offset i.e. 1234.
So if we represent the memory as an array memory[] then memory[1234] = 1.
The value of the variable is the data 1 and the reference to it is its offset 1234.
What number the computer chooses depends on lots of things that don't concern us. All I'm saying is that the data 1 will be stored somewhere, and that byte where it's stored will have a number.
When we talk about references we basically mean the number of the byte where the data is stored. So when we do x = 1 the variable x will have the value 1 and id(x) = 1234.
That is, the name x is a reference set to 1234, and when we want the value of x we look in memory byte 1234 and pull out the data stored there.
In a way, if you'd started with C this would be easier because in C the difference between an address and a value is very clear. Python tries to hide this from you to make life simpler, but then when you do need to look more closely you find yourself wondering what is going on.
@JohnRennie It's the difference between foo1() and foo1 You can try this at the console. Give me a moment to try it ... >>> def foo1(): ... print("This function prints foo1") ... >>> foo1 <function foo1 at 0x0000023CC40370D8> >>> foo1() This function prints foo1 There. You can try this for yourself. First I define the function foo1. Now if I just type foo1 this is an object called foo1 containing a function, so Pyhton shows <function foo1 at 0x0000023CC40370D8> which is an internal representation of the function.
locs = [ [1], [2] ]
print(locs)
for loc in locs:
print("Before loc = " + str(id(loc)))
loc = []
print("After loc = " + str(id(loc)))
print(locs)
I get:
D:\rhs\Python>python test.py
[[1], [2]]
Before loc = 1957092217416
After loc = 1957122400200
Before loc = 1957092217928
After loc = 1957122400200
[[1], [2]]
So when you do loc = [] it is creating a new empty list and assigning the address of that new list to the variable loc. It is not changing the original list.
The way a generator works is that when you call it it creates an interator but doesn't actually do anything. You need to use a for loop to actually run it.
What the call to mygen() returns is actually an iterator, and the for loop steps through this iterator just like for i in [1,2,3] iterates through the list.