python_ reflection: Application

class User(object):

    DEF denglu (Self):
         Print ( ' Welcome to the login page! ' )

    DEF zhuce (Self):
         Print ( ' Welcome to the registration page! ' )

    DEF youke (Self):
         Print ( ' Welcome visitor page! ' )

u = User()
while True:
    Q = INPUT ( ' Enter content (dengli or zhuce or youke): ' )
     IF the hasattr (U, Q):
        func = getattr(u,q)
        func()

The program will achieve call different functions depending on the function of the input string.

 

 

 

Guess you like

Origin www.cnblogs.com/wangdianchao/p/12003788.html