question about scope

James Stroud jstroud at mbi.ucla.edu
Sun Oct 1 01:54:08 EDT 2006


John Salerno wrote:
> Steve Holden wrote:
> 
>> The methods do indeed look in their enclosing class, but only for 
>> self-relative references. These are sought first in the instance, then 
>> in the instance's class, then in the instance's class's superclass, 
>> and so on up to the ultimate superclass. In other words, all attribute 
>> lookup uses the method resolution order ...
> 
> 
> So what I did is correct? It does work, but why don't I have to define 
> the list as self.menu_items as well?

This is because that list is an attribute of the class. Instances have a 
reference of this class attribute, but it can be replaced by an 
attribute of the instance with self (self is a reference to the instance 
and not the class. This example might help:

py> class C(object):
...   value = 42
...   def separate_from_pack(self, some_value):
...     self.value = some_value
...
py> C.value
42
py> c1 = C()
py> c1.value
42
py> c2 = C()
py> c2.value
42
py> c2.separate_from_pack(88)
py> c2.value
88
py> C.value
42
py> c3 = C()
py> c3.value
42


James

--
James Stroud
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