new-style-class

Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object

一个人想着一个人 提交于 2019-11-27 14:38:56
问题 I have the following Python 2.7 code: 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() I'm trying to extend the __init__() method so that when I instantiate an 'Eye' it does a bunch of other stuff (self.some_other_defined_stuff()), in addition to what Frame sets up. Frame.__init__() needs to run first. I get the following error: super(Eye, self).__init__() TypeError: must be type

What is the purpose of subclassing the class “object” in Python?

只愿长相守 提交于 2019-11-27 04:09:53
All the Python built-ins are subclasses of object and I come across many user-defined classes which are too. Why? What is the purpose of the class object ? It's just an empty class, right? In short, it sets free magical ponies. In long, Python 2.2 and earlier used "old style classes". They were a particular implementation of classes, and they had a few limitations (for example, you couldn't subclass builtin types). The fix for this was to create a new style of class. But, doing this would involve some backwards-incompatible changes. So, to make sure that code which is written for old style

Difference between type(obj) and obj.__class__

故事扮演 提交于 2019-11-26 22:27:26
What is the difference between type(obj) and obj.__class__ ? Is there ever a possibility of type(obj) is not obj.__class__ ? I want to write a function that works generically on the supplied objects, using a default value of 1 in the same type as another parameter. Which variation, #1 or #2 below, is going to do the right thing? def f(a, b=None): if b is None: b = type(a)(1) # #1 b = a.__class__(1) # #2 Mark Roddy type(obj) and type.__class__ do not behave the same for old style classes: >>> class a(object): ... pass ... >>> class b(a): ... pass ... >>> class c: ... pass ... >>> ai=a() >>> bi

Why does @foo.setter in Python not work for me?

一个人想着一个人 提交于 2019-11-26 15:05:36
So, I'm playing with decorators in Python 2.6, and I'm having some trouble getting them to work. Here is my class file: class testDec: @property def x(self): print 'called getter' return self._x @x.setter def x(self, value): print 'called setter' self._x = value What I thought this meant is to treat x like a property, but call these functions on get and set. So, I fired up IDLE and checked it: >>> 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

What is the purpose of subclassing the class “object” in Python?

五迷三道 提交于 2019-11-26 11:05:57
问题 All the Python built-ins are subclasses of object and I come across many user-defined classes which are too. Why? What is the purpose of the class object ? It\'s just an empty class, right? 回答1: In short, it sets free magical ponies. In long, Python 2.2 and earlier used "old style classes". They were a particular implementation of classes, and they had a few limitations (for example, you couldn't subclass builtin types). The fix for this was to create a new style of class. But, doing this

Difference between type(obj) and obj.__class__

戏子无情 提交于 2019-11-26 07:38:00
问题 What is the difference between type(obj) and obj.__class__ ? Is there ever a possibility of type(obj) is not obj.__class__ ? I want to write a function that works generically on the supplied objects, using a default value of 1 in the same type as another parameter. Which variation, #1 or #2 below, is going to do the right thing? def f(a, b=None): if b is None: b = type(a)(1) # #1 b = a.__class__(1) # #2 回答1: type(obj) and type.__class__ do not behave the same for old style classes: >>> class

Why does @foo.setter in Python not work for me?

柔情痞子 提交于 2019-11-26 04:10:42
问题 So, I\'m playing with decorators in Python 2.6, and I\'m having some trouble getting them to work. Here is my class file: class testDec: @property def x(self): print \'called getter\' return self._x @x.setter def x(self, value): print \'called setter\' self._x = value What I thought this meant is to treat x like a property, but call these functions on get and set. So, I fired up IDLE and checked it: >>> from testDec import testDec from testDec import testDec >>> t = testDec() t = testDec() >>

What is the difference between old style and new style classes in Python?

三世轮回 提交于 2019-11-25 21:39:00
问题 What is the difference between old style and new style classes in Python? When should I use one or the other? 回答1: From http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes : Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x , but type(x) is always <type 'instance'> . This reflects