instance vs class attributes

James Stroud jstroud at mbi.ucla.edu
Tue Mar 29 20:50:14 EST 2005


On Tuesday 29 March 2005 05:37 pm, James Stroud wrote:
> > 1) What are the benefits of using a member variable without the 'self'
> >    qualifier? (I think this is because you can use _x without an
> >    instance of A().)
>
> No such thing as a benefit here. self.a inside a method is the same as a
> outside (see code below for what I mean).

By the way, here is some unintuitive behavior to think about:

>>> class bob:
...   a = 2
...   def get_a(self):
...     return self.a
...   def set_a(self,an_a):
...     self.a = an_a
...
>>> bob1 = bob()
>>> bob1.a
2
>>> bob1.set_a(4)
>>> bob1.a
4
>>> bob.a = 14
>>> bob1.a
4
>>> bob2 = bob()
>>> bob2.a
14
>>> bob.a = 99
>>> bob2.a
99
>>> bob1.a
4

James

-- 
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list