Python scoping rules when it comes to classes are so confusing.
Can you guess what would be output of the following program?
x = 1
class Foo:
print(x)
x = x + 1
print(x)
print(x, Foo.x)
Now take the same piece of code and put it in a function.
def f():
x = 1
class Foo:
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 yo
On Tue, Dec 4, 2012 at 9:38 AM, Satyajit Ranjeev
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
You are right in mentioning scopes.
Lets take case 2:
def f():
x = 1# x is pointing to object 1
class Foo:
print(x) # what you are doing is printing object 1
x = x + 1# you are defining x in the class's scope and pointing
it to the object 2
On Tue, Dec 4, 2012 at 10:13 AM, Satyajit Ranjeev
wrote:
> You are right in mentioning scopes.
>
> Lets take case 2:
>
> def f():
>x = 1# x is pointing to object 1
>
>class Foo:
>print(x) # what you are doing is printing object 1
>x = x + 1#
Alembic is good enough. At least, I learnt to handle alter table, drop
columns and add columns.
Thanks.
On Sun, Dec 2, 2012 at 7:28 PM, Kracekumar Ramaraju
wrote:
> Alembic http://alembic.readthedocs.org/en/latest/front.html written by
> Mike
> Bayer author of SQLAlchemy, I really like it. Appr
Alembic can do automatic migration but you need to be careful.
On Tue, Dec 4, 2012 at 10:34 AM, Gopalakrishnan Subramani <
gopalakrishnan.subram...@gmail.com> wrote:
> Alembic is good enough. At least, I learnt to handle alter table, drop
> columns and add columns.
>
> Thanks.
>
>
> On Sun, Dec 2
>
> What is really weird is that the class body is evaluated without
> considering the enclosed scope, but the methods defined in the class
> have access to the enclosed scope.
Found the culprit. Lets looks at the following code.
code = """
x = x+1
def f():
return x
print(x, f())
"""
x = 5
e
On 04-Dec-2012, at 10:23 AM, Anand Chitipothu wrote:
> On Tue, Dec 4, 2012 at 10:13 AM, Satyajit Ranjeev
> wrote:
>> You are right in mentioning scopes.
>>
>> Lets take case 2:
>>
>> def f():
>> x = 1# x is pointing to object 1
>>
>> class Foo:
>> print(x) #