python - 如何在 Python 中从 __init__ 返回一个值?

标签 python class init

我有一个带有 __init__ 函数的类。

创建对象时如何从该函数返回整数值?

我写了一个程序,其中 __init__ 进行命令行解析,我需要设置一些值。可以将其设置在全局变量中并在其他成员函数中使用吗?如果是这样怎么做?到目前为止,我在类外声明了一个变量。并将其设置为一个功能不会反射(reflect)在其他功能中??

最佳答案

你为什么要这样做?

如果你想在调用类时返回一些其他对象,那么使用 __new__() 方法:

class MyClass(object):
    def __init__(self):
        print "never called in this case"
    def __new__(cls):
        return 42

obj = MyClass()
print obj

关于python - 如何在 Python 中从 __init__ 返回一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2491819/

相关文章:

python - 具有时变狄利克雷边界条件的非稳态扩散-平流方程

Javascript 替换图像源

linux - initctl 中的作业是什么意思以及如何使用它?

objective-c - 在 initWithNibName 中调用 addSubview : causes viewDidLoad (and other UI Object inits)to fire before the addSubview Call executes

python - python语言中列表理解的两个列表

python - 在python2.6上有条件地安装importlib

python - 您正在使用 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径

class - 如何在 Fortran 2003 中以多态方式解除分配?

class - 如何在 ReasonML 中为 Web 组件扩展 JavaScript HTMLElement 类?

linux - Upstart init 正在泄漏内存,你如何调试它?