1

MyClass has two methods defined: methodA and methodB.

After declaring foo instance of MyClass I proceed with declaring a variable first_method_a which is foo object's methodA.

foo = MyClass()
first_method_a = foo.methodA

Next, I go ahead an loose foo variable left myself only with variable first_method_a which still points to foo.methodA and it is functional.

foo = None
first_method_a()

Which prints:

this is method A

Now, how can I run methodB only having first_method_a variable?

class MyClass(object):
    def __init__(self):
        super(MyClass, self).__init__()

    def methodA(self):
        print 'this is method A'

    def methodB(self):
        print 'this is method B'


foo = MyClass()
first_method_a = foo.methodA

foo = None

first_method_a()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

0 Answers0