For questions about various details related to Python data model: built-in types, classes, metaclasses, magic __dunder__ methods, operators, object initialization, attribute lookup, etc. Always remember to use 'python' tag together with this one. Using specific tag like 'operators' or 'metaclass' when appropriate is encouraged.
Questions tagged [python-datamodel]
99 questions
6
votes
1 answer
Why is the __init__ method of Counter referred to as a descriptor?
I was reading the __init__ method of the Counter class, and saw this:
if not args:
TypeError("descriptor '__init__' of 'Counter' object "
"needs an argument")
I wasn't sure what it meant by descriptor, so I checked the python data…

Davis Yoshida
- 1,757
- 1
- 10
- 24
6
votes
4 answers
Proper return value for __len__ for an object that acts as an infinite sequence
I am writing a python class that acts as an immutable, infinite sequence.
The class does not use a generator to access this infinite sequence, rather it generates a value for an arbitrary index in the sequence according to an algorithm within the…

trevorKirkby
- 1,886
- 20
- 47
5
votes
6 answers
How do you know when looking at the list of attributes and methods listed in a dir which are attributes and which are methods?
I am working through trying to learn to program in Python and am focused on getting a better handle on how to use Standard and other modules. The dir function seems really powerful in the interpreter but I wonder if I am missing something because…

PyNEwbie
- 4,882
- 4
- 38
- 86
5
votes
2 answers
Python dunder methods wrapped as property
I stumbled upon this code that I found weird as it seems to violate the fact that python builtins call dunder methods directly from the class of the object. Using __call__ as an example, if we define class A as following:
class A:
@property
…

Zhang Daniel
- 79
- 8
5
votes
2 answers
How can I get the file name of the function that is passed to my decorator in python?
I want to get the original file/scriptname, etc of the function that is being decorated. How can I do that?
def decorate(fn):
def wrapped():
return "scriptname: " + fn.scriptname?
return wrapped
I tried using…

theRealWorld
- 1,188
- 2
- 8
- 23
4
votes
4 answers
Looping over a Python / IronPython Object Methods
What is the proper way to loop over a Python object's methods and call them?
Given the object:
class SomeTest():
def something1(self):
print "something 1"
def something2(self):
print "something 2"

BuddyJoe
- 69,735
- 114
- 291
- 466
4
votes
7 answers
Is there a way to access the formal parameters if you implement __getattribute__
It seems as though __getattribute__ has only 2 parameters (self, name).
However, in the actual code, the method I am intercepting actually takes arguments.
Is there anyway to access those arguments?
Thanks,
Charlie
Charles Reich
4
votes
6 answers
Python introspection: How to get an 'unsorted' list of object attributes?
The following code
import types
class A:
class D:
pass
class C:
pass
for d in dir(A):
if type(eval('A.'+d)) is types.ClassType:
print d
outputs
C
D
How do I get it to output in the order in which these classes…

molicule
- 5,481
- 3
- 29
- 40
4
votes
1 answer
Why doesn't str() use __getattribute__ to get __str__ and how to produce the effect?
When I call str() on an object that has an overloaded __getattribute__ method it doesn't seem to use it and instead calls __str__ directly. Is there some other functionality I should be modifying or way to get it to use __getattribute__? If I…

RedKnite
- 1,525
- 13
- 26
3
votes
1 answer
Python Data Model : confused again with classmethod
It's said that :
When it would yield a class method object, it is transformed into a
bound user-defined method object whose im_class and im_self attributes
are both C.
in the Reference
And I did an EX.
>>> class C(object) :
... …

Determinant
- 3,886
- 7
- 31
- 47
3
votes
1 answer
Python Data Model Document : an unbound user-defined method object and a class method object
In Data Model of the reference, writer spent lots of effort explaining how User-defined methods are created and operated:(See http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy and roll down)
User-defined method objects may…

Determinant
- 3,886
- 7
- 31
- 47
3
votes
2 answers
Wrapping derived class method from base class
Imagine I have a base and a derived class like so:
class A:
def foo(self):
pass
class B(A):
def foo(self):
pass
I wish to wrap calls foo calls made by instances of B. Modifying any part of B is not allowed (I don’t own…

xrisk
- 3,790
- 22
- 45
3
votes
1 answer
Overwrite * and ** unpacking with datamodel methods?
I'm trying to figure out how to give an object the ability to unpack values.
The use case I came up with is the following:
Let's have an Interval class, which we'll use to evaluate real valued functions.
We would like to ask for
Membership, hence…

ekiim
- 792
- 1
- 11
- 25
3
votes
1 answer
Why do __setattr__ and __delattr__ raise an AttributeError in this case?
In Python, what is the rationale for which object.__setattr__ and type.__setattr__ raise an AttributeError during attribute update if the type has an attribute which is a data descriptor without a __set__ method? Likewise, what is the rationale for…

Géry Ogam
- 6,336
- 4
- 38
- 67
3
votes
1 answer
What is `operator.__inv__` existing for?
What is the difference with inv and invert?
>>> import operator
>>> operator.inv is operator.invert
False
>>> operator.__inv__ is operator.__invert__
False
I gather that __invert__ is the hook for the unary ops like ~1 or (1).__invert__().
But what…

X Æ A-Xiii
- 576
- 4
- 14