Programmatically turn module/set of functions into a Python class

前端 未结 4 1021
时光说笑
时光说笑 2021-01-21 22:28

Suppose I have a file with a bunch methods as bunch_methods.py:

def one(x):
  return int(x)

def two(y)
  return str(y)

Is there a way to take th

4条回答
  •  借酒劲吻你
    2021-01-21 23:00

    I don't know why you would want this, as you can already use the module as a class, but anyway:

    import bunch_methods as bm
    
    print bm.one('1')
    print bm.two(1)
    
    class BunchClass:
        def __init__(self, methods):
            self.__dict__.update(methods.__dict__)
    
    bc = BunchClass(bm)
    
    print bc.one('2')
    print bc.two(2)
    

提交回复
热议问题