如何在python中定义类外的成员函数?

时间:2018-06-10 13:22:28

标签: python

import numpy as np

class MyClass:

        a = int()
        b = float()
        c = np.array([0 for i in range 10])

        def Memberfunction():

            print("Hii")

1 个答案:

答案 0 :(得分:0)

class test_class():
    def __init__(self, some_func):
        self.some_value = 2    
        setattr(type(self), some_func.__name__, some_func)

def my_requested_func(self):
    print(self.some_value)

class_instance = test_class(my_requested_func)
class_instance.my_requested_func()

这应该有用,但我没有看到你想要这样做的任何理由。