标签: new-style-class

Python中的旧样式和新样式类有什么区别?

Python中的旧样式和新样式类有什么区别?我什么时候应该使用其中一种?

python oop types class new-style-class

953
推荐指数
8
解决办法
22万
查看次数

为什么Python中的@ foo.setter不适合我?

所以,我正在使用Python 2.6中的装饰器,我在使它们工作时遇到了一些麻烦.这是我的班级文件:

class testDec:

    @property
    def x(self): 
        print 'called getter'
        return self._x

    @x.setter
    def x(self, value): 
        print 'called setter'
        self._x = value
Run Code Online (Sandbox Code Playgroud)

我认为这意味着x像对待财产一样,但是在get和set上调用这些函数.所以,我解雇了IDLE并检查了它:

>>> from testDec import testDec
from testDec import testDec
>>> t = testDec()
t = testDec()
>>> t.x
t.x
called getter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "testDec.py", line 18, in x
    return self._x
AttributeError: testDec instance has no attribute '_x'
>>> t.x = 5
t.x = 5
>>> …
Run Code Online (Sandbox Code Playgroud)

python decorator new-style-class

153
推荐指数
4
解决办法
7万
查看次数

在Python中继承类"对象"的目的是什么?

所有Python内置object函数都是子类,我遇到了许多用户定义的类.为什么?这堂课的目的是object什么?这只是一个空洞的课,对吧?

python object deprecated future-proof new-style-class

52
推荐指数
2
解决办法
1万
查看次数

type(obj)和obj .__ class__之间的区别

type(obj)和之间有什么区别obj.__class__?有可能type(obj) is not obj.__class__吗?

我想编写一个在提供的对象上一般工作的函数,使用与另一个参数相同类型的默认值1.下面的#1或#2哪个变体会做正确的事情?

def f(a, b=None):
  if b is None:
    b = type(a)(1) # #1
    b = a.__class__(1) # #2
Run Code Online (Sandbox Code Playgroud)

python new-style-class

44
推荐指数
4
解决办法
7503
查看次数

为什么Python新样式类中的__new__不是类方法?

Python 2.2的Changelog(引入了新式类)说明了关于该__new__函数的以下内容:

__new__是一个静态方法,而不是类方法.我最初认为它必须是一个类方法,这就是我添加classmethod原语的原因.不幸的是,使用类方法,upcalls在这种情况下不能正常工作,因此我不得不将它作为静态方法,并将显式类作为其第一个参数.

但是,我无法想到为什么类方法不能用于此目的,它肯定会更好看.为什么最终不会__new__以类方法结束?当Guido说"在这种情况下上调不能正常工作"时,Guido会提到什么?

python language-design new-operator new-style-class

35
推荐指数
1
解决办法
1957
查看次数

Python:总是使用__new__而不是__init__?

我了解都__init____new__工作.我想知道是否有什么__init__可以做到的__new__不可以?

即可以使用__init__以下模式替换:

class MySubclass(object):
    def __new__(cls, *args, **kwargs):
        self = super(MySubclass, cls).__new__(cls, *args, **kwargs)
        // Do __init__ stuff here
        return self
Run Code Online (Sandbox Code Playgroud)

我问,因为我想让Python OO的这个方面更适合我.

python new-style-class

27
推荐指数
2
解决办法
4540
查看次数

当父不从对象继承时,Python 2.x super __init__继承不起作用

我有以下Python 2.7代码:

class Frame:
    def __init__(self, image):
        self.image = image

class Eye(Frame):
    def __init__(self, image):
        super(Eye, self).__init__()
        self.some_other_defined_stuff()
Run Code Online (Sandbox Code Playgroud)

我正在尝试扩展该__init__()方法,以便当我实例化一个'Eye'时,除了Frame设置之外,还会做一堆其他的东西(self.some_other_defined_stuff()).Frame.__init__()需要先跑.

我收到以下错误:

super(Eye, self).__init__()
TypeError: must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

其中我不明白其逻辑原因.有人可以解释一下吗?我习惯于在红宝石中输入'super'.

python inheritance super new-style-class python-2.7

25
推荐指数
2
解决办法
5万
查看次数

Python 2.7中的旧式和新式类?

可能重复:
Python中的旧样式和新样式类

Python 2.7中新式和旧式类的当前状态是什么?我不经常使用Python,但我依旧记得这个问题.文档似乎根本没有提到这个问题:Python教程:类.我还需要担心吗?一般来说,我应该声明我的类:

class MyClass:
    pass
Run Code Online (Sandbox Code Playgroud)

要么?

class MyClass(object):
    pass
Run Code Online (Sandbox Code Playgroud)

python class new-style-class python-2.7

24
推荐指数
2
解决办法
7618
查看次数

使用super调用多个父类的init?

可能重复:
Super可以处理多重继承吗?

Python继承?我有一个类结构(下面),并希望子类调用__init__父双方.这可能是以"超级"方式做的还是只是一个可怕的想法?

class Parent1(object):
    def __init__(self):
        self.var1 = 1

class Parent2(object):
    def _init__(self):
        self.var2 = 2

class Child(Parent1, Parent2):
    def __init__(self):
        ## call __init__ of Parent1
        ## call __init__ of Parent2
        ## super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)

python init multiple-inheritance super new-style-class

14
推荐指数
3
解决办法
1万
查看次数

Python在父类中使用派生类的方法?

我可以强制父类调用派生类的函数版本吗?

class Base(object):
    attr1 = ''
    attr2 = ''

    def virtual(self):
        pass               # doesn't do anything in the parent class

    def func(self):
        print "%s, %s" % (self.attr1, self.attr2)
        self.virtual()
Run Code Online (Sandbox Code Playgroud)

以及从中衍生出来的一类

class Derived(Base):
    attr1 = 'I am in class Derived'
    attr2 = 'blah blah'

    def virtual(self):
        # do stuff...
        # do stuff...
Run Code Online (Sandbox Code Playgroud)

清除模糊:

d = Derived()
d.func()         # calls self.virtual() which is Base::virtual(), 
                 #  and I need it to be Derived::virtual()
Run Code Online (Sandbox Code Playgroud)

python inheritance new-style-class

11
推荐指数
2
解决办法
1万
查看次数