translating PHP to Python

Eric Nieuwland eric.nieuwland at xs4all.nl
Mon Feb 6 02:00:03 EST 2006


Dave wrote:
> class A(object):
>     def create_child(self):
>         self.child = B()
>         self.child.do_stuff(self)
>
> class B(object):
>     def do_stuff(self, parent):
>         self.parent = parent
>         if self.parent.__class__.__name__ == 'A':
>             print "I'm a child of an A!"
>         else:
>             print "Well, I'm a motherless child. Does that mean I can
> kill Macbeth?"

Depending on your actual needs you could change that to:

class A(object):
     def create_child(self):
         self.child = B(self)

class B(object):
     def __init__(self, parent):
	self.do_stuff(parent)

     def do_stuff(self, parent):
         self.parent = parent
         if self.parent.__class__.__name__ == 'A':
             print "I'm a child of an A!"
         else:
             print "I know ye not!"

which IMHO makes it clearer from A's perspective.

--eric




More information about the Python-list mailing list