Questions tagged [new-style-class]

47 questions
1
vote
1 answer

Why does setter behave differently in new-style class and old-style class

Using python 2.7, suppose I have a Test class with the new-style class syntax defined below. class Test(object): def __init__(self): self._a = 5 @property def a(self): return self._a @a.setter def a(self, val): self._a =…
InfinityPlus1
  • 91
  • 1
  • 12
1
vote
3 answers

Why does isinstance(nonnewstyle, object) return true in Python 2.X?

I am reading a textbook on Python and it covers new-style class differences. Observe the following code in Python 2.X in the interpreter, which uses classic classes: class C: pass X = C() isinstance(X, object) #returns true isinstance(C, object)…
user3751940
  • 123
  • 4
1
vote
1 answer

Why favor object.__setattr__(self, name, value) only in new style classes?

According to Python 2.7.12 documentation: If __setattr__() wants to assign to an instance attribute, it should not simply execute self.name = value — this would cause a recursive call to itself. Instead, it should insert the value in the…
nalzok
  • 14,965
  • 21
  • 72
  • 139
1
vote
0 answers

Confused with python attribute getting process: infinite recursion?

Suppose we want to get an object's attribute: smth = object.attr. Also suppose we already know what object our attribute is located in, let it be the class called A. As far as I know, the attribute getting procedure then looks like this: attr =…
Alexander Lutsenko
  • 2,130
  • 8
  • 14
1
vote
2 answers

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

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(…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
1 answer

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

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…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
0 answers

My method doesn't get called when I (mis?)use super

class Document(object): def __init__(self,filename): self.filename = filename self.edits = 0 def write(self,content): print "D.write" fd = open(self.filename,"w") fd.write(content) …
ychaouche
  • 4,922
  • 2
  • 44
  • 52
1
vote
3 answers

Making super() work in Python's urllib2.Request

This afternoon I spent several hours trying to find a bug in my custom extension to urllib2.Request. The problem was, as I found out, the usage of super(ExtendedRequest, self), since urllib2.Request is (I'm on Python 2.5) still an old style class,…
Boldewyn
  • 81,211
  • 44
  • 156
  • 212
1
vote
3 answers

Get class object __dict__ without special attributes

For getting all the defined class attributes I try to go with TheClass.__dict__ but that also gives me the special attributes. Is there a way to get only the self-defined attributes or do I have to "clean" the dict myself?
desolat
  • 4,123
  • 6
  • 36
  • 47
0
votes
2 answers

How to set up styleClass inside ts file?

I want to create css element inside ts file and pass it as a styleClass to PrimeNg Toast. var style = document.createElement('style'); style.innerHTML = '::ng-deep .cssClass { background-color: #e62323; border: solid #8A427A; border-width: 0 0 0…
0
votes
2 answers

Is __dict__.update() oldstyle?

I'm getting confused reading about the differences in newstyle vs oldstyle attributes. In this example: Is this code using old-style method to modify attributes? In Event() at self.__dict__.update(kw) Do I use getattr, getattr or getattribute ?…
ninMonkey
  • 7,211
  • 8
  • 37
  • 66
0
votes
1 answer

How to verify if class implements a rich comparison method?

With the old style classes simply using hasattr works: >>> class old: ... pass ... >>> hasattr(old, '__eq__') False Using the new style classes every class has the attribute __eq__: >>> class nsc(object): ... pass ... >>> hasattr(nsc,…
hugos
  • 1,313
  • 1
  • 10
  • 19
0
votes
0 answers

Descriptors don't act like descriptors when set to an instance -- why not?

I have an enumerated descriptor which I want to be able to add to classes dynamically. Here it is class TypedEnum: '''Class to make setting of enum types valid and error checked''' def __init__(self, enum, default=None): self._enum =…
vitiral
  • 8,446
  • 8
  • 29
  • 43
0
votes
2 answers

What is the preferred Python 3.x class definition syntax?

As Is it necessary or useful to inherit from python's object in Python 3.x? and Python class inherits object make clear, it is no longer necessary to inherit from object when defining a class in Python 3. As a corollary to this which isn't directly…
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
0
votes
2 answers

What's a simple utility function to differentiate between an old-style and a new-style python class or object

What's a simple utility function to differentiate between an old-style and a new-style python class or object? Are the following correct/complete: isNewStyle1 = lambda o: isinstance(hasattr(o, '__class__') and o.__class__ or o, type) isNewStyle2 =…
khosrow
  • 8,799
  • 4
  • 21
  • 24