Questions tagged [new-style-class]
47 questions
6
votes
1 answer
Is subclassing from object the same as defining type as metaclass?
This is an old-style class:
class OldStyle:
pass
This is a new-style class:
class NewStyle(object):
pass
This is also a new-style class:
class NewStyle2:
__metaclass__ = type
Is there any difference whatsoever between NewStyle and…

zvone
- 18,045
- 3
- 49
- 77
6
votes
2 answers
Python object @property
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 =…

cdwilson
- 4,310
- 4
- 26
- 32
5
votes
3 answers
importing from within a python Abtract Factory
I want to create an Abstract Factory in order to abstract hardware differences between computers (say a RaspberryPi and an Arduino) in Python 2.7.
I am using the following implementation of an Abstract Factory:
'''
Provide a device-agnostic…

sagism
- 881
- 4
- 11
- 18
5
votes
2 answers
How can we force getattribute() to be called for magic methods (special methods)?
The python documentation states that __getattribute__ may be bypassed when looking up special methods. This is the result of implicit invocation via language syntax or built-in functions.
For example,
elem = container[0]
is not the same as:
elem =…

Toothpick Anemone
- 4,290
- 2
- 20
- 42
5
votes
2 answers
Descriptors and python-provided attributes
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)…

Flavien
- 7,497
- 10
- 45
- 52
5
votes
2 answers
Old-style classes, new-style classes and metaclasses
In Python 2.x, all new-style classes inherit from object implicitly or explicitly. Then look at this:
>>> class M(type):
... pass
...
>>> class A:
... __metaclass__ = M
...
>>> class B:
... pass
...
>>> a = A()
>>> b = B()
>>>…

Alcott
- 17,905
- 32
- 116
- 173
4
votes
2 answers
Python's property decorator does not work as expected
class Silly:
@property
def silly(self):
"This is a silly property"
print("You are getting silly")
return self._silly
@silly.setter
def silly(self, value):
print("You are making silly…

Locke
- 341
- 5
- 15
4
votes
1 answer
type of class in python
why if I do:
class C(): pass
type(C())
I got: , but if I do:
class C(object): pass
type(c())
I got: ?
The first is not very userfull

Ruggero Turra
- 16,929
- 16
- 85
- 141
4
votes
1 answer
How can I make a read-only property mutable?
I have two classes, one with an "in-place operator" override (say +=) and another that exposes an instance of the first through a @property. (Note: this is greatly simplified from my actual code to the minimum that reproduces the problem.)
class…

JuSTMOnIcAjUSTmONiCAJusTMoNICa
- 122
- 2
- 10
3
votes
0 answers
what is the difference between object and type in python
We have two types of classes in Python 2.x as all know like Old style classes and new Style classes
class OldStyle:
pass
The type of instances of Oldstyle classes is always instance
class NewStyle(object):
pass
New style classes have an…

Shiva Krishna Bavandla
- 25,548
- 75
- 193
- 313
3
votes
1 answer
What is the purpose of Python's property.getter?
Since Python strives for there to be one right way, I'm wondering what the purpose of property.getter is. In this example WhyMe defines a getter but Other doesn't so I'm wondering what the point of property.getter is over just using property.
class…

Pat C
- 403
- 3
- 10
2
votes
2 answers
Python new-style-class-related question
I am a python learner and currently hacking up a class with variable number of fields as in the "Bunch of Named Stuff" example here.
class Bunch:
def __init__(self, **kwds):
self.__dict__.update(kwds)
I also want to write a __setattr__…

amit kumar
- 20,438
- 23
- 90
- 126
2
votes
2 answers
New style class attribute search for builtin operations
Consider the following difference between classic classes and the new style classes.
class A():
data = 'abcd'
def __getattr__(self, name):
return getattr(self.data, name)
class B(object):
data = 'abcd'
…

rogue-one
- 11,259
- 7
- 53
- 75
2
votes
1 answer
What happens when you call object.__new__?
Old-style class
class Person():
_names_cache = {}
def __init__(self,name):
self.name = name
def __new__(cls,name):
return cls._names_cache.setdefault(name,object.__new__(cls,name))
ahmed1 = Person("Ahmed")
ahmed2 =…

ychaouche
- 4,922
- 2
- 44
- 52
2
votes
1 answer
Why does overriding __getattribute__ to proxy a value screw up isinstance?
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)
…

Claudiu
- 224,032
- 165
- 485
- 680