开发者

Is there any way to do this without using '__init__'? [closed]

开发者 https://www.devze.com 2023-01-01 17:28 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_JS百科 Closed 12 years ago.
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()

how to call the 'b' method not in the __init__

thanks

the error is :

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


I would use a property instead:

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()


If you want a().c to always return a().d + 1, then use property as suggested by Olivier. However, if you want a and it's derived classes to have a class attribute that's value is dynamically set to +1 of the declared (or inherited) value of c in the class, then you can use a metaclass.

def meta(name, bases, class_locals):
    class_locals['c'] = class_locals.get('d', 0) + 1
    return type.__new__(name, bases, class_locals)

class A(object):
    __metaclass__ = meta
    d = 4

    def __init__(self):
        print self.c

class B(A):
    d = 5


>>> A()
5
>>> B()
6
>>> print A.c
5
>>> print B.c
6


You have defined b as in instance method (a "regular" method). Such methods can only be called on an instance of the class.

In your code you attempt to call the "b" method inside the class definition. Inside a class definition you can only call static-methods and class-methods of that class, but not instance-methods. I recommend reading about the classmethod and staticmethod decorators.

An example, to give you a push in the right direction:

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


You can't do that directly. Firstly as Francesco says you cannot call the method b without an instance to call it on. You can change the method into a classmethod but that needs an instance of the class, which doesn't exist until you reach the end of the class definition.

I think the closest you can get is to make b a classmethod and initialise c after the class has been definined:

class a(object):
    d = 4
    def __init__(self):
        print self.c

    @classmethod
    def b(cls):
        return cls.d+1

a.c = a.b()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号