defining class functions

James Mills prologic at shortcircuit.net.au
Mon Jan 19 19:31:58 EST 2009


On Tue, Jan 20, 2009 at 10:08 AM, Astan Chee <astan.chee at al.com.au> wrote:
> Hi,
> I have two classes in python that are in two different files/python scripts.
> Class A uses Class B like this:
> class B(object):
>   def function1(self,something):
>       pass
>   def function2(self,something):
>       print "hello one"
>       print something
>
> class A(object):
>   def __init__(self):
>         instance = B()
>         instance.function2("hello two")
>         self.function3()
>   def function3(self):
>         print "hello three"

def function3(self):
   print "hello three"
   self.instance.function1 = lambda x; x

But you must bind instnace to self in B

Modify your __init__ as follows:

class A(object):
   def __init__(self):
      self.instance = B()
      self.instance.function2("hello two")
      self.function3()

What's the use-case anyway ?
There might be a better way to solve your problem :)

cheers
James



More information about the Python-list mailing list