0

I mean , for example, in the following code I want to call prin in pre by name, but how?

class a(object):

def __init__(self):

    self.zz=1

    self.aa='hello'

def prin(self):

    print 'hello'

def pre(self,name):

    #if name is 'prin' then call self.prin

if __name__ == '__main__':

az = a()

az.pre('prin')`
Crazymage
  • 114
  • 9

1 Answers1

0

getattr

def pre(self,name):
    # name being 'prin' in your case
    if hasattr(self, name):
        getattr(self, name)()
Brian
  • 7,394
  • 3
  • 25
  • 46