- 论坛徽章:
- 0
|
本帖最后由 nakar 于 2011-08-14 10:50 编辑
推广到string转变量。
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- class class1(object):
- def __init__(self):
- self.s1 = 'add_no_argu'
- self.s2 = 'add_argu'
- self.d1 = ['a','b','c']
- self.d2 = 'd1'
-
- def add_no_argu(self):
- print 'no argu'
-
- def add_argu(self,a,b):
- return a+b
- def test(self):
- m1 = getattr(self, self.s1) #string转method
- m1()
- m2 = getattr(self, self.s2)
- print m2(1,2)
- m3 = getattr(self,self.d2) #string转变量
- print m3
- c = class1()
- c.test()
复制代码 输出信息
- no argu
- 3
- ['a', 'b', 'c']
复制代码 |
|