[BangPypers] wierd class behavior
Anand Chitipothu
anandology at gmail.com
Tue Dec 4 05:12:47 CET 2012
On Tue, Dec 4, 2012 at 9:38 AM, Satyajit Ranjeev
<satyajit.ranjeev at gmail.com> wrote:
> It is the way Python handles objects. Unlike variables in C/C++ where a variable can point to an address location in the memory Python uses variables to point to an object.
>
> Now in the first case what you are doing is pointing x to the object 1 in x=1.
> When you print x it just prints 1. When you try to assign x to x+1 you are pointing x in the class's scope to a new object which is x + 1 or 2. And that's why you get the weird results.
Well, *the class scope* is quite different from function scope.
The same code that works in a class, fails in a function.
x = 1
class Foo:
x = x + 1
def f():
x = x + 1
>
> The other cases can be expanded on the same basis.
Did you try the other ones?
Anand
More information about the BangPypers
mailing list