new-style-class

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

微笑、不失礼 提交于 2019-12-05 01:20:40
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 but it'll greatly help with future updates. So assuming I need C to be the metaclass of B and B will be a

calling init for multiple parent classes with super? [duplicate]

点点圈 提交于 2019-12-04 16:35:45
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class structure (below), and want the child class to call the __init__ of both parents. Is this possible to do in a 'super' way or is it just a terrible idea? 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

Python using derived class's method in parent class?

大憨熊 提交于 2019-12-03 12:26:39
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::virtual(), # and I need it to be Derived::virtual() If you instantiate a Derived (say d = Derived() ), the

calling init for multiple parent classes with super? [duplicate]

馋奶兔 提交于 2019-12-03 10:37:36
This question already has answers here : Can Super deal with multiple inheritance? (2 answers) Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class structure (below), and want the child class to call the __init__ of both parents. Is this possible to do in a 'super' way or is it just a terrible idea? 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__()

How to tidy/fix PyCXX's creation of new-style Python extension-class?

若如初见. 提交于 2019-12-02 09:45:54
问题 I've nearly finished rewriting a C++ Python wrapper (PyCXX). The original allows old and new style extension classes, but also allows one to derive from the new-style classes: import test // ok a = test.new_style_class(); // also ok class Derived( test.new_style_class() ): def __init__( self ): test_funcmapper.new_style_class.__init__( self ) def derived_func( self ): print( 'derived_func' ) super().func_noargs() def func_noargs( self ): print( 'derived func_noargs' ) d = Derived() The code

How to tidy/fix PyCXX's creation of new-style Python extension-class?

风格不统一 提交于 2019-12-02 04:20:33
I've nearly finished rewriting a C++ Python wrapper (PyCXX). The original allows old and new style extension classes, but also allows one to derive from the new-style classes: import test // ok a = test.new_style_class(); // also ok class Derived( test.new_style_class() ): def __init__( self ): test_funcmapper.new_style_class.__init__( self ) def derived_func( self ): print( 'derived_func' ) super().func_noargs() def func_noargs( self ): print( 'derived func_noargs' ) d = Derived() The code is convoluted, and appears to contain errors ( Why does PyCXX handle new-style classes in the way it

Why isn't __new__ in Python new-style classes a class method?

假装没事ソ 提交于 2019-11-29 20:48:39
The Changelog for Python 2.2 (where new-style classes were introduced) says the following about the __new__ function: __new__ is a static method, not a class method. I initially thought it would have to be a class method, and that's why I added the classmethod primitive. Unfortunately, with class methods, upcalls don't work right in this case, so I had to make it a static method with an explicit class as its first argument. However, I cannot think of why class methods wouldn't work for this purpose, and it would certainly look better. Why didn't __new__ end up as a class method in the end?

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

做~自己de王妃 提交于 2019-11-28 22:50:44
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, not classobj Which I do not understand the logical cause of. Can someone explain please? I'm used to

Why isn't __new__ in Python new-style classes a class method?

佐手、 提交于 2019-11-28 16:27:43
问题 The Changelog for Python 2.2 (where new-style classes were introduced) says the following about the __new__ function: __new__ is a static method, not a class method. I initially thought it would have to be a class method, and that's why I added the classmethod primitive. Unfortunately, with class methods, upcalls don't work right in this case, so I had to make it a static method with an explicit class as its first argument. However, I cannot think of why class methods wouldn't work for this

Python: always use __new__ instead of __init__?

夙愿已清 提交于 2019-11-28 06:48:55
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. So, the class of a class is typically type , and when you call Class() the __call__() method on Class 's class handles that. I believe type.__call__() is implemented more or less