Re: Python class gotcha with scope?

2009-06-20 Thread billy
great, thanks for the quick responses :)


On Jun 21, 2:41 am, Carl Banks  wrote:
> On Jun 20, 11:32 pm, billy  wrote:
>
> > I don't quite understand why this happens. Why doesn't b have its own
> > version of r? If r was just an int instead of a dict, then it would.
>
> > >>> class foo:
>
> > ...     r = {}
> > ...     def setn(self, n):
> > ...             self.r["f"] = n
> > ...>>> a = foo()
> > >>> a.setn(4)
>
> > >>> b = foo()
> > >>> b.r
>
> > {'f': 4}
>
> r is a class attribute, that is, it is attacted to the class itself.
> Look at what happens when you enter foo.r at the interactive prompt:
>
> >>> foo.r
>
> {'f': 4}
>
> You want an instance attribute, a value attached to the instance of
> the class.  You create those in the __init__ method:
>
> class foo:
>     def __init__(self):
>         self.r = {}
>     def setn(self,n):
>         self.r["n"] = n
>
> Carl Banks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class gotcha with scope?

2009-06-20 Thread Carl Banks
On Jun 20, 11:32 pm, billy  wrote:
> I don't quite understand why this happens. Why doesn't b have its own
> version of r? If r was just an int instead of a dict, then it would.
>
> >>> class foo:
>
> ...     r = {}
> ...     def setn(self, n):
> ...             self.r["f"] = n
> ...>>> a = foo()
> >>> a.setn(4)
>
> >>> b = foo()
> >>> b.r
>
> {'f': 4}

r is a class attribute, that is, it is attacted to the class itself.
Look at what happens when you enter foo.r at the interactive prompt:

>>> foo.r
{'f': 4}


You want an instance attribute, a value attached to the instance of
the class.  You create those in the __init__ method:


class foo:
def __init__(self):
self.r = {}
def setn(self,n):
self.r["n"] = n



Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class gotcha with scope?

2009-06-20 Thread Vincent
On Jun 21, 2:38 pm, Vincent  wrote:
> On Jun 21, 2:32 pm, billy  wrote:
>
>
>
> > I don't quite understand why this happens. Why doesn't b have its own
> > version of r? If r was just an int instead of a dict, then it would.
>
> > >>> class foo:
>
> > ...     r = {}
> > ...     def setn(self, n):
> > ...             self.r["f"] = n
> > ...>>> a = foo()
> > >>> a.setn(4)
>
> > >>> b = foo()
> > >>> b.r
>
> > {'f': 4}
>
> > thanks,
>
> > billy
>
> class Foo:
>     def __init__(self):
>         self.r = {}
>     def setn(self,n):
>         self.r['f'] = n
>
> a = Foo()
> a.setn(3)
> a.r
> {'f': 3}
> b = Foo()
> b.r
> {}

you defined r as class-level variable.
and i defined r as instance-level variable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class gotcha with scope?

2009-06-20 Thread Vincent
On Jun 21, 2:32 pm, billy  wrote:
> I don't quite understand why this happens. Why doesn't b have its own
> version of r? If r was just an int instead of a dict, then it would.
>
> >>> class foo:
>
> ...     r = {}
> ...     def setn(self, n):
> ...             self.r["f"] = n
> ...>>> a = foo()
> >>> a.setn(4)
>
> >>> b = foo()
> >>> b.r
>
> {'f': 4}
>
> thanks,
>
> billy


class Foo:
def __init__(self):
self.r = {}
def setn(self,n):
self.r['f'] = n

a = Foo()
a.setn(3)
a.r
{'f': 3}
b = Foo()
b.r
{}
-- 
http://mail.python.org/mailman/listinfo/python-list


Python class gotcha with scope?

2009-06-20 Thread billy
I don't quite understand why this happens. Why doesn't b have its own
version of r? If r was just an int instead of a dict, then it would.

>>> class foo:
... r = {}
... def setn(self, n):
... self.r["f"] = n
...
>>> a = foo()
>>> a.setn(4)
>>>
>>> b = foo()
>>> b.r
{'f': 4}

thanks,

billy
-- 
http://mail.python.org/mailman/listinfo/python-list