[Tutor] Inner Class access to outer class attributes?

Alan Gauld alan.gauld at freenet.co.uk
Thu Mar 23 23:10:38 CET 2006


I'm not sure why you think this needs to be an inner class?
Why not just make it a peer class of GroupLocation?

Then you can pass in the GroupLocation as a parent to 
the SubLocation constructor. The sublocation methods 
then reference the GroupLocation via the parent attribute:

class Container(list):
    def __init__(self)
         name = 'foo'
         # do stuff here

class Content(object):
    def __init__(self, parent):
        self.parent = parent
        self.action = 'foo'
    def doThings(self):
        print parent.name

c = Container()
for n in range(3):
    c.append(Content(c))
    c[n].doThings()

result = [n.doThings() for n in c if n.action == 'foo']

Inner classes are one of those esoteric OO features that sound 
awfully clever but in practice are hardly ever needed! 
IMHO at least :-)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




----- Original Message ----- 
From: "stv" <stvsmth at gmail.com>
To: <Tutor at python.org>
Sent: Thursday, March 23, 2006 8:17 PM
Subject: [Tutor] Inner Class access to outer class attributes?


# So I figure out inner classes while typing the
# first draft of this email, at least mostly
# How do I access the "outer" class attributes from
# the inner class?

class GroupLocation(list):     ### Note subclass of list

  class _Sublocation(object):
    def __init__(self, sublocation, action):
      self.sublocation = sublocation
      self.action = action
    def do_stuff(self):
      ###
      ### How would I get to GroupLocation attributes?
      ###
      print GroupLocation.name, self.sublocation

  def __init__(self, name, group_location):
    self.name = name
    self.group_location = group_location

  def add_sublocation(self, sublocation, action):
    self.append(GroupLocation._Sublocation(sublocation, action)

group = GroupLocation('group1name', 'group1location')
group.add_sublocation('sublocation1','foo')
group.add_sublocation('sublocation2','bar')
group.add_sublocation('sublocation3','zip')
group.add_sublocation('sublocation4','foo')

[sub.do_stuff() for sub in group if sub.action == 'foo']

# output (if it worked):
#   Group1location sublocation1
#   Group1location sublocation4
#
# instead I get this:
# AttributeError: type object 'GroupLocation' has no attribute 'name'


# Now, I suppose I could start down this road:
class GroupLocation(object):
  def __init__(self,name, group_location):
    self.name = name
    self.group_location = group_location
    self.sublocations = []
  def add_sublocation(self,sublocation, action):
    self.sublocations.append((sublocation, action))

group = GroupLocation('group1', 'group1location')
group.add_sublocation('sublocation1','foo')
group.add_sublocation('sublocation2','bar')
group.add_sublocation('sublocation3','zip')
group.add_sublocation('sublocation4','foo')

for sub in group.sublocations:
if sub[1] == 'foo':
  print group.group_location, sub[0]

# output:
#   Group1location sublocation1
#   Group1location sublocation4

# But that feels wrong in several ways:

# 1) Any time I want to do_stuff() I have
# to retype

# 2) sub[1] & sub [0] are confusing after
# not reading the code for a few hours.




More information about the Tutor mailing list