【发布时间】:2018-10-09 12:55:10
【问题描述】:
我是一个新的 python 用户。因此,这可能非常愚蠢。但是自动运行一个类(里面有几个函数)并返回给定值的结果的最佳方法是什么。例如:
class MyClass():
def __init__(self,x):
self.x=x
def funct1(self):
return (self.x)**2
##or any other function
def funct2(self,y):
return y/100.0
##or any other function
def wrapper(self):
y=self.funct1()
z=self.funct2(y)
##or other combination of functions
return z
现在要运行它,我正在使用:
run=MyClass(5)
run.wrapper()
但我想这样跑:
MyClass(5)
这将返回一个值并且可以保存在变量中而无需使用包装函数。
【问题讨论】:
-
如果你不想保留它的实例就不要使用一个类,它似乎是你想要的一个函数
-
1.您可以在
__init__中调用wrapper(但请记住,__init__不能返回值,因此您仍然需要使用属性来存储结果)。 2. 我不确定MyClass是否值得/应该是一门课。
标签: python python-2.7