is it possible to include a new method to existing object at instance initialization?
本问题已经有最佳答案,请猛点这里访问。
这是我在Python3.4中的尝试:
1 2 3 4 5 6 7 8 9 10 | import imp def my_example_function(): print("my_example_function:") class my_class: def __init__(self): module = imp.load_source(my_example_function, __file__) my_class_instance = my_class() |
结果是:
1 2 3 4 5 6 7 8 9 10 | Traceback (most recent call last): File"D:\Python34\test.py", line 10, in <module> my_class_instance = my_class() File"D:\Python34\test.py", line 8, in __init__ module = imp.load_source(my_example_function, __file__) File"D:\python34\lib\imp.py", line 166, in load_source spec = util.spec_from_file_location(name, pathname, loader=loader) File"<frozen importlib._bootstrap>", line 932, in spec_from_file_location File"<frozen importlib._bootstrap>", line 1439, in is_package AttributeError: 'function' object has no attribute 'rpartition' |
它看起来像是某种内部错误,是Python本身的一个bug。
错误表明"function"-对象没有"rpartition"属性。您提供给