Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法不使用'__初始化&';?_Python - Fatal编程技术网

Python 有没有办法不使用'__初始化&';?

Python 有没有办法不使用'__初始化&';?,python,Python,如何调用不在\uuuu init\uuuu中的“b”方法 谢谢 错误是: class a(object): c=b()# how to call the b method d=4 def __init__(self): print self.c def b(self): return self.d+1 a() 回溯(最近一次呼叫最后一次): 文件“D:\zjm_code\a.py”,第12行,在 a类(对象): 文件“D:\z

如何调用不在
\uuuu init\uuuu
中的“b”方法

谢谢

错误是:

class a(object):
    c=b()# how to call the b method 
    d=4
    def __init__(self):
        print self.c
    def b(self):
        return self.d+1

a()
回溯(最近一次呼叫最后一次):
文件“D:\zjm_code\a.py”,第12行,在
a类(对象):
文件“D:\zjm_code\a.py”,第13行,在
c=b()#如何调用b方法
名称错误:未定义名称“b”

您已将b定义为实例内方法(“常规”方法)。此类方法只能在类的实例上调用

在代码中,您试图在类定义内调用“b”方法。在类定义中,只能调用该类的静态方法和类方法,而不能调用实例方法。我建议阅读有关classmethod和staticmethod装饰器的内容

举个例子,向正确的方向推:

Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 12, in <module>
    class a(object):
  File "D:\zjm_code\a.py", line 13, in a
    c=b()# how to call the b method 
NameError: name 'b' is not defined

您已经将b定义为实例方法(“常规”方法)。此类方法只能在类的实例上调用

在代码中,您试图在类定义内调用“b”方法。在类定义中,只能调用该类的静态方法和类方法,而不能调用实例方法。我建议阅读有关classmethod和staticmethod装饰器的内容

举个例子,向正确的方向推:

Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 12, in <module>
    class a(object):
  File "D:\zjm_code\a.py", line 13, in a
    c=b()# how to call the b method 
NameError: name 'b' is not defined

你不能直接这么做。首先,正如Francesco所说,如果没有实例来调用方法b,就不能调用它。您可以将该方法更改为classmethod,但这需要一个类的实例,该实例在到达类定义的末尾之前不存在

我认为你能得到的最接近的方法是将b设为classmethod,并在定义类后初始化c:

class A(object):
    d = 42
    c = b()

    @classmethod
    def b(klass): # "class" is a Python keyword so we can't use it here
        return klass.d + 1

你不能直接这么做。首先,正如Francesco所说,如果没有实例来调用方法b,就不能调用它。您可以将该方法更改为classmethod,但这需要一个类的实例,该实例在到达类定义的末尾之前不存在

我认为你能得到的最接近的方法是将b设为classmethod,并在定义类后初始化c:

class A(object):
    d = 42
    c = b()

    @classmethod
    def b(klass): # "class" is a Python keyword so we can't use it here
        return klass.d + 1
我会使用一个:

我会使用一个:

如果希望
a().c
始终返回
a().d+1
,请使用属性。但是,如果希望
a
并且它的派生类有一个class属性,该属性的值被动态设置为类中
c
的声明(或继承)值的+1,那么可以使用元类

class a(object):
    d=4
    def __init__(self):
        print self.c
    def b(self):
        return self.d+1
    c = property(b)

a()
a.c # returns a.b()
如果希望
a().c
始终返回
a().d+1
,请使用属性。但是,如果希望
a
并且它的派生类有一个class属性,该属性的值被动态设置为类中
c
的声明(或继承)值的+1,那么可以使用元类

class a(object):
    d=4
    def __init__(self):
        print self.c
    def b(self):
        return self.d+1
    c = property(b)

a()
a.c # returns a.b()

注意,因为b是一个方法(self是它的第一个参数),你不能像b()那样没有任何参数调用它,你需要一个a的实例才能让它工作。注意,因为b是一个方法(self是它的第一个参数),你不能像b()那样没有任何参数调用它,你需要一个a的实例才能让它工作。很好地解释了OP想要的东西。=]破解OP想要的东西做得很好。=]