Python中的旧样式和新样式类有什么区别?我什么时候应该使用其中一种?
所以,我正在使用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内置object函数都是子类,我遇到了许多用户定义的类.为什么?这堂课的目的是object什么?这只是一个空洞的课,对吧?
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 2.2的Changelog(引入了新式类)说明了关于该__new__函数的以下内容:
__new__是一个静态方法,而不是类方法.我最初认为它必须是一个类方法,这就是我添加classmethod原语的原因.不幸的是,使用类方法,upcalls在这种情况下不能正常工作,因此我不得不将它作为静态方法,并将显式类作为其第一个参数.
但是,我无法想到为什么类方法不能用于此目的,它肯定会更好看.为什么最终不会__new__以类方法结束?当Guido说"在这种情况下上调不能正常工作"时,Guido会提到什么?
我了解都__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 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中的旧样式和新样式类
Python 2.7中新式和旧式类的当前状态是什么?我不经常使用Python,但我依旧记得这个问题.文档似乎根本没有提到这个问题:Python教程:类.我还需要担心吗?一般来说,我应该声明我的类:
class MyClass:
pass
Run Code Online (Sandbox Code Playgroud)
要么?
class MyClass(object):
pass
Run Code Online (Sandbox Code Playgroud) 可能重复:
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) 我可以强制父类调用派生类的函数版本吗?
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) new-style-class ×10
python ×10
class ×2
inheritance ×2
python-2.7 ×2
super ×2
decorator ×1
deprecated ×1
future-proof ×1
init ×1
new-operator ×1
object ×1
oop ×1
types ×1