Questions tagged [new-style-class]

47 questions
1124
votes
8 answers

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? When should I use one or the other?
readonly
  • 343,444
  • 107
  • 203
  • 205
168
votes
4 answers

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

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…
TR.
  • 2,205
  • 3
  • 16
  • 15
83
votes
5 answers

Difference between type(obj) and obj.__class__

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…
Roger Pate
67
votes
6 answers

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

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?
ignoramus
44
votes
1 answer

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

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…
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
29
votes
3 answers

Python: always use __new__ instead of __init__?

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): …
EoghanM
  • 25,161
  • 23
  • 90
  • 123
29
votes
4 answers

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

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…
cjm2671
  • 18,348
  • 31
  • 102
  • 161
26
votes
2 answers

Old-style and new-style classes in Python 2.7

Possible Duplicate: Old style and new style classes in Python What is the current state of affairs with new-style and old-style classes in Python 2.7? I don't work with Python often, but I vaguely remember the issue. The documentation doesn't…
User
  • 62,498
  • 72
  • 186
  • 247
23
votes
3 answers

calling init for multiple parent classes with super?

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…
scruffyDog
  • 721
  • 3
  • 7
  • 17
11
votes
2 answers

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

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(): …
citronic
  • 9,868
  • 14
  • 51
  • 74
11
votes
2 answers

Python using derived class's method in parent class?

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): …
user35288
11
votes
4 answers

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

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…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
10
votes
1 answer

How do I call a property setter from __init__

I have the following chunk of python code: import hashlib class User: def _set_password(self, value): self._password = hashlib.sha1(value).hexdigest() def _get_password(self): return self._password password =…
Chris Smith
  • 251
  • 2
  • 6
8
votes
2 answers

Using a class instance as a class attribute, descriptors, and properties

I have recently stated trying to use the newer style of classes in Python (those derived from object). As an excersise to familiarise myself with them I am trying to define a class which has a number of class instances as attributes, with each of…
Chris
  • 44,602
  • 16
  • 137
  • 156
6
votes
3 answers

How to import object from builtins affecting just one class?

I am converting code from python2 to python3 for newstyle classes using future. My project is in Django 1.11 I have a class in forms.py as: class Address: ...rest of code... class AddressForm(Address, forms.ModelForm): ...rest of…
Deesha
  • 538
  • 8
  • 27
1
2 3 4