python3.x - 关于python3.5使用exec动态调用方法获取返回值的问题:单独执行好使,但是封装在class里就会报错?
阿神
阿神 2017-04-18 10:05:30
0
1
791
阿神
阿神

闭关修行中......

reply all(1)
Ty80

Regarding the use of exec, the questioner can refer to another question: https://segmentfault.com/q/10...

To give you a solution to your current problem:

# encoding: utf-8

class tess(object):
    def __init__(self):
        self.__globals__ = {}
        self.__locals__ = self.__globals__

    def ex(self):
        sa = 'a=1'
        exec(sa, self.__globals__, self.__locals__)
        print(self.__globals__['a'])

s = tess()
s.ex()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!