new-style-class

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

Why isn't __new__ in Python new-style classes a class method? Question: 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 …

Total answers: 1

Python: always use __new__ instead of __init__?

Python: always use __new__ instead of __init__? Question: 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 …

Total answers: 3

What is the purpose of subclassing the class "object" in Python?

What is the purpose of subclassing the class "object" in Python? Question: 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? Asked By: ignoramus || Source Answers: Right, but it marks …

Total answers: 6

Difference between type(obj) and obj.__class__

Difference between type(obj) and obj.__class__ Question: 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 …

Total answers: 5

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

Why does @foo.setter in Python not work for me? Question: 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 …

Total answers: 4

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

What is the difference between old style and new style classes in Python? Question: What is the difference between old style and new style classes in Python? When should I use one or the other? Asked By: readonly || Source Answers: From New-style and classic classes: Up to Python 2.1, old-style classes were the only …

Total answers: 8