new-style-class

Why does PyCXX handle new-style classes in the way it does?

孤街浪徒 提交于 2019-12-31 04:44:05
问题 I'm picking apart some C++ Python wrapper code that allows the consumer to construct custom old style and new style Python classes from C++. The original code comes from PyCXX, with old and new style classes here and here. I have however rewritten the code substantially, and in this question I will reference my own code, as it allows me to present the situation in the greatest clarity that I am able. I think there would be very few individuals capable of understanding the original code

How to check if object is instance of new-style user-defined class?

拥有回忆 提交于 2019-12-22 04:06:16
问题 Code: import types class C(object): pass c = C() print(isinstance(c, types.InstanceType)) Output: False What correct way to check if object is instance of user-defined class for new-style classes? UPD: I want put additional emphasize on if checking if type of object is user-defined . According to docs: types.InstanceType The type of instances of user-defined classes. UPD2: Alright - not "correct" ways are OK too. UPD3: Also noticed that there is no type for set in module types 回答1: You can

How to check if object is instance of new-style user-defined class?

房东的猫 提交于 2019-12-22 04:05:03
问题 Code: import types class C(object): pass c = C() print(isinstance(c, types.InstanceType)) Output: False What correct way to check if object is instance of user-defined class for new-style classes? UPD: I want put additional emphasize on if checking if type of object is user-defined . According to docs: types.InstanceType The type of instances of user-defined classes. UPD2: Alright - not "correct" ways are OK too. UPD3: Also noticed that there is no type for set in module types 回答1: You can

Python using derived class's method in parent class?

徘徊边缘 提交于 2019-12-21 03:52:21
问题 Can I force a parent class to call a derived class's version of a function? 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() and a class that derives from it class Derived(Base): attr1 = 'I am in class Derived' attr2 = 'blah blah' def virtual(self): # do stuff... # do stuff... Clearing up vagueness: d = Derived() d.func() # calls self.virtual() which is Base:

Python: always use __new__ instead of __init__?

守給你的承諾、 提交于 2019-12-17 18:23:29
问题 I understand how both __init__ and __new__ work. I'm wondering if there is anything __init__ can do that __new__ cannot? i.e. can use of __init__ be replaced by the following pattern: class MySubclass(object): def __new__(cls, *args, **kwargs): self = super(MySubclass, cls).__new__(cls, *args, **kwargs) // Do __init__ stuff here return self I'm asking as I'd like to make this aspect of Python OO fit better in my head. 回答1: So, the class of a class is typically type , and when you call Class()

Why does overriding __getattribute__ to proxy a value screw up isinstance?

大憨熊 提交于 2019-12-11 03:09:06
问题 Why does this happen? class IsInstanceScrewer(object): def __init__(self, value): self.value = value def __getattribute__(self, name): if name in ('value',): return object.__getattribute__(self, name) value = object.__getattribute__(self, 'value') return object.__getattribute__(value, name) isinstance(IsInstanceScrewer(False), bool) #True isinstance(IsInstanceScrewer([1, 2, 3]), list) #True The class is definitely not an instance of bool, even though it attempts to wrap it. 回答1: _

Descriptors and python-provided attributes

冷暖自知 提交于 2019-12-10 13:44:15
问题 I am learning Python, and I am trying to understand descriptors better. When I look at this Python online book: http://www.cafepy.com/article/python_attributes_and_methods/ch01s05.html, it says: If attrname is a special (i.e. Python-provided) attribute for objectname, return it. I don't understand what Python-provided means. Can someone give me an exemple of such Python-provided attribute that would take precedence over the usual resolution order? Note: I am only interested in new-style

Python object @property

瘦欲@ 提交于 2019-12-07 03:21:12
问题 I'm trying to create a point class which defines a property called "coordinate". However, it's not behaving like I'd expect and I can't figure out why. class Point: def __init__(self, coord=None): self.x = coord[0] self.y = coord[1] @property def coordinate(self): return (self.x, self.y) @coordinate.setter def coordinate(self, value): self.x = value[0] self.y = value[1] p = Point((0,0)) p.coordinate = (1,2) >>> p.x 0 >>> p.y 0 >>> p.coordinate (1, 2) It seems that p.x and p.y are not getting

TypeError: Error when calling the metaclass bases a new-style class can't have only classic bases

主宰稳场 提交于 2019-12-06 19:58:12
问题 A collection of classes defined as: class A(): @staticmethod def call(): print('a') class C(type): def __repr__(self): return 'somename' class B(A): __metaclass__ = C @staticmethod def call(): print('b') def boundcall(self): print('bound') When run, gives this error: TypeError: Error when calling the metaclass bases a new-style class can't have only classic bases I need the metaclass (I think) to have a known string representation of B in my code. Reason for having that is beside the point

Python object @property

亡梦爱人 提交于 2019-12-05 08:35:54
I'm trying to create a point class which defines a property called "coordinate". However, it's not behaving like I'd expect and I can't figure out why. class Point: def __init__(self, coord=None): self.x = coord[0] self.y = coord[1] @property def coordinate(self): return (self.x, self.y) @coordinate.setter def coordinate(self, value): self.x = value[0] self.y = value[1] p = Point((0,0)) p.coordinate = (1,2) >>> p.x 0 >>> p.y 0 >>> p.coordinate (1, 2) It seems that p.x and p.y are not getting set for some reason, even though the setter "should" set those values. Anybody know why this is? The