Re: [Tutor] super() and inherited attributes?

2005-06-28 Thread jfouhy
Quoting Alan G [EMAIL PROTECTED]:

 I don't know the direct answer but the more common way 
 of doing that in Python is not to use super() but just 
 call the inherited constructor directly:
 
 Parent.__init__(self,'I am a child')
 
 
 SO if you just want to fix the itch use that, if you want 
 to understand super() then that won't help and its over to 
 someone else! :-)

OTOH, there are reasons for prefering super over a direct call.

This explains it better than I could:
http://www.python.org/2.2.3/descrintro.html#cooperation

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] super() and inherited attributes?

2005-06-27 Thread Marcus Goldfish
Hi,

The following example doesn't work as I would like-- the child
instance doesn't expose the attribute set in the parent.  Can someone
point out what I am missing?

Thanks,
Marcus


class Parent(object):
   def __init__(self, name=I am a parent):
  self.name = name

class Child(Parent):
   def __init__(self, number):
  super(Parent, self).__init__(I am a child)
  self.number = number

# I would like it to produce the following:
 c = Child(23)
 c.number
23
 c.name
I am a child

# but I 'AttributeError: 'Child' object has no attribute 'name''
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] super() and inherited attributes?

2005-06-27 Thread Brian van den Broek
Marcus Goldfish said unto the world upon 28/06/2005 00:58:
 Hi,
 
 The following example doesn't work as I would like-- the child
 instance doesn't expose the attribute set in the parent.  Can someone
 point out what I am missing?
 
 Thanks,
 Marcus
 
 
 class Parent(object):
def __init__(self, name=I am a parent):
   self.name = name
 
 class Child(Parent):
def __init__(self, number):
   super(Parent, self).__init__(I am a child)
   self.number = number
 
 # I would like it to produce the following:
 
c = Child(23)
c.number
 
 23
 
c.name
 
 I am a child
 
 # but I 'AttributeError: 'Child' object has no attribute 'name''

Hi Marcus,

Try it this way:

  class Parent(object):
 def __init__(self, name=I am a parent):
 self.name = name


  class Child(Parent):
 def __init__(self, number):
 # Note change here in super()
 super(Child, self).__init__(Finally, I'm a child!)
 self.number = number


  c = Child(42)
  c.number
42
  c.name
Finally, I'm a child!


Take a look at these classes, and perhaps they will help clear up any 
residual puzzlement.

  class A(object):
 def do_it(self):
 print From A


  class B(A):
 def do_it(self):
 print From B


  class C(B):
 def do_it(self):
# look for do_it in B's superclass (i.e. A)
super(B, self).do_it()

# look for do_it in C's superclass (i.e. B)
super(C, self).do_it()


  c = C()
  c.do_it()
 From A
 From B
 

HTH,

Brian vdB


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor