python newbie - question about lexical scoping
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Dec 2 11:59:08 EST 2007
Matt Barnicle a écrit :
>>>On Dec 1, 4:47 pm, Matt Barnicle <ma... at wageslavery.org> wrote:
>>
>>aye yaye aye... thanks for the pointers in the right direction.. i
>>fiddled around with the code for a while and now i've reduced it to the
>>*real* issue... i have a class dict variable that apparently holds its
>>value across instantiations of new objects..
If it's a class attribute, it's indeed shared between all instances...
>> the problem can be
>>illustrated in the following much simpler code:
>>
>>>>>class foo():
>>
>>... bar = { 'baz': 'bing' }
(snip)
> ok, i see... python has a concept i'm not accustomed to which i found
> described here:
>
> http://zephyrfalcon.org/labs/python_pitfalls.html
> 4. Class attributes vs instance attributes
>
> so i'm sure what is going on is obvious to experienced python
> programmers... i'm not really sure how to get around this though.
It's not a problem:
class Foo(object):
def __init__(self):
self.bar = {'baz':'bing'}
>i'll
> need to spend some time on reworking our models code i guess... i
> inherited this from someone, and what he was trying to do was to set
> default values for objects representing tables (in kind of a simple ORM
> layer) and storing the values in a dict, and when the object is
> instantiated, the table is queried and the default dict values are
> overwritten.
class Foo(object):
bar = {'baz':'bing'}
def __init__(self):
self.bar = self.bar
More information about the Python-list
mailing list