Questions tagged [python-datamodel]

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.

84 questions
6178
votes
21 answers

What are metaclasses in Python?

In Python, what are metaclasses and what do we use them for?
e-satis
  • 515,820
  • 103
  • 283
  • 322
1656
votes
10 answers

Getting the class name of an instance?

How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived? Was thinking maybe the inspect module might have helped me…
Dan
  • 29,553
  • 22
  • 54
  • 82
1120
votes
25 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
  • 45,682
  • 27
  • 72
  • 79
277
votes
15 answers

How to get method parameter names?

Given the Python function: def a_method(arg1, arg2): pass How can I extract the number and names of the arguments. I.e., given that I have a reference to func, I want the func.[something] to return ("arg1", "arg2"). The usage scenario for this…
Staale
  • 24,584
  • 22
  • 63
  • 85
154
votes
11 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…
Hanno S.
  • 2,631
  • 3
  • 17
  • 27
124
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 a call like v[4] where v is a vector python…
nicotine
  • 2,169
  • 3
  • 17
  • 15
107
votes
5 answers

Should __ne__ be implemented as the negation of __eq__ in Python?

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
  • 44,586
  • 38
  • 140
  • 186
94
votes
6 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,134
  • 7
  • 41
  • 72
87
votes
12 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
  • 26,293
  • 28
  • 81
  • 123
74
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.
  • 13,553
  • 9
  • 51
  • 82
66
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,148
  • 20
  • 64
  • 87
40
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
  • 306,152
  • 101
  • 198
  • 201
38
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…
Aaron Hall
  • 291,450
  • 75
  • 369
  • 312
28
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
  • 266,989
  • 79
  • 484
  • 630
25
votes
1 answer

How python attribute lookup process works?

When i say "python attribute lookup proccess" i mean: what python does when you write x.foo?? Searching the web i didn't found to much docs about this, one of the best papers i found resumed the proccess to the following steps (you can see the full…
Ariel
  • 1,615
  • 1
  • 18
  • 27
1
2 3 4 5 6