[Tutor] How to cal la method when have classes inside another class

Blake Winton bwinton at latte.ca
Fri Aug 13 15:21:36 CEST 2004


Alan Gauld wrote:
>>class Class1:
>>   def someMethod():
>>     pass
>>   class Class2:
>>     def otherMethod():
>>        # ???call of someMethod???
>>I want to call someMethod in otherMethod. How can I do this?

I'm going to assume that you really want nested classes, and point you 
at the following page:
http://www.brpreiss.com/books/opus7/html/page598.html

Which says:
The methods of a nested class may access the instance attributes of the 
nested class instance but not of any outer class instance.

Or, in other words:
You can't.

(There's a little more information here:
  http://mail.python.org/pipermail/python-list/2002-February/085830.html
)

> If so I don't know of any way for a methjod of the nested class to
> call a method of the outer class - the outer class is invisible to the
> inner.  But I confess I've never had a need to use inner classes in Python.
> Why do you feel you need to resort to this? What are you trying to do that
> requires it?

This is probably the most important question.  A lot of the times I need 
to use nested or inner classes in Java are made unnecessary by Python. 
Perhaps if you show us what you really want to do (as opposed to how you 
want to do it), we can offer other, more Pythonic suggestions.

Later,
Blake.



More information about the Tutor mailing list