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
7262
votes
25 answers
What are metaclasses in Python?
What are metaclasses? What are they used for?

Bite code
- 578,959
- 113
- 301
- 329
1918
votes
12 answers
Getting the class name of an instance
How do I find out the name of the class used to create an instance of an object in Python?
I'm not sure if I should use the inspect module or parse the __class__ attribute.

Dan
- 33,953
- 24
- 61
- 87
1448
votes
31 answers
Is there a built-in function to print all the current properties and values of an object?
So what I'm looking for here is something like PHP's print_r function.
This is so I can debug my scripts by seeing what's the state of the object in question.

fuentesjr
- 50,920
- 27
- 77
- 81
349
votes
20 answers
How to get method parameter names?
Given that a function a_method has been defined like
def a_method(arg1, arg2):
pass
Starting from a_method itself, how can I get the argument names - for example, as a tuple of strings, like ("arg1", "arg2")?

Staale
- 27,254
- 23
- 66
- 85
192
votes
13 answers
Get fully qualified class name of an object in Python
For logging purposes I want to retrieve the fully qualified class name of a Python object. (With fully qualified I mean the class name including the package and module name.)
I know about x.__class__.__name__, but is there a simple method to get the…

Hannah S.
- 3,081
- 3
- 20
- 27
139
votes
5 answers
Implementing slicing in __getitem__
I am trying to implement slice functionality for a class I am making that creates a vector representation.
I have this code so far, which I believe will properly implement the slice but whenever I do something like v[4] where v is a vector, python…

nicotine
- 2,519
- 3
- 19
- 15
127
votes
4 answers
Should __ne__ be implemented as the negation of __eq__?
I have a class where I want to override the __eq__ method. It seems to make sense that I should override the __ne__ method as well. Should I implement __ne__ as the negation of __eq__ as such or is it a bad idea?
class A:
def __init__(self,…

Falmarri
- 47,727
- 41
- 151
- 191
119
votes
13 answers
Get class that defined method
How can I get the class that defined a method in Python?
I'd want the following example to print "__main__.FooClass":
class FooClass:
def foo_method(self):
print "foo"
class BarClass(FooClass):
pass
bar = BarClass()
print…

Jesse Aldridge
- 7,991
- 9
- 48
- 75
108
votes
13 answers
Prevent creating new attributes outside __init__
I want to be able to create a class (in Python) that once initialized with __init__, does not accept new attributes, but accepts modifications of existing attributes. There's several hack-ish ways I can see to do this, for example having a…

astrofrog
- 32,883
- 32
- 90
- 131
75
votes
2 answers
Is everything greater than None?
Is there a Python built-in datatype, besides None, for which:
>>> not foo > None
True
where foo is a value of that type? How about Python 3?

Attila O.
- 15,659
- 11
- 54
- 84
69
votes
11 answers
How is the 'is' keyword implemented in Python?
... the is keyword that can be used for equality in strings.
>>> s = 'str'
>>> s is 'str'
True
>>> s is 'st'
False
I tried both __is__() and __eq__() but they didn't work.
>>> class MyString:
... def __init__(self):
... self.s = 'string'
... …

Srikanth
- 11,780
- 23
- 72
- 92
47
votes
5 answers
How do you check whether a python method is bound or not?
Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?

readonly
- 343,444
- 107
- 203
- 205
36
votes
2 answers
What is the relationship between the Python data model and built-in functions?
As I read Python answers on Stack Overflow, I continue to see some people telling users to use the data model's special methods or attributes directly.
I then see contradicting advice (sometimes from myself) saying not to do that, and instead to use…

Russia Must Remove Putin
- 374,368
- 89
- 403
- 331
30
votes
2 answers
Schrödinger's variable: the __class__ cell magically appears if you're checking for its presence?
There's a surprise here:
>>> class B:
... print(locals())
... def foo(self):
... print(locals())
... print(__class__ in locals().values())
...
{'__module__': '__main__', '__qualname__': 'B'}
>>>…

wim
- 338,267
- 99
- 616
- 750
27
votes
1 answer
Which Python dunder/magic methods do you need to implement to correctly proxy an object?
I'm trying to create an object proxy.
Attribute/property lookup can be done by simply implementing the __getattribute__, __setattr__ and __delattr__ methods.
However, other functionalities like len(x), x[], bool(x) require other dunder methods like…

Pragy Agarwal
- 578
- 1
- 8
- 22