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
24
votes
5 answers

How Do I Perform Introspection on an Object in Python 2.x?

I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property. Similarly, I'd like to…
Metahyperbolic
17
votes
2 answers

Python - class __hash__ method and set

I'm using set() and __hash__ method of python class to prevent adding same hash object in set. According to python data-model document, set() consider same hash object as same object and just add them once. But it behaves different as below: …
Aida.Mirabadi
  • 779
  • 3
  • 8
  • 20
17
votes
4 answers

When where and how can i change the __class__ attr of an object?

I'd like to be able to do: >>> class a(str): ... pass ... >>> b = a() >>> b.__class__ = str Traceback (most recent call last): File "", line 1, in TypeError: __class__ assignment: only for heap types
Juanjo Conti
  • 25,163
  • 37
  • 101
  • 128
12
votes
0 answers

List of all Python dunder methods - Which ones 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…
12
votes
4 answers

Remove elements as you traverse a list in Python

In Java I can do by using an Iterator and then using the .remove() method of the iterator to remove the last element returned by the iterator, like this: import java.util.*; public class ConcurrentMod { public static void main(String[] args) { …
user102008
  • 28,544
  • 10
  • 78
  • 100
11
votes
2 answers

Multiply operator applied to list(data structure)

I'm reading How to think like a computer scientist which is an introductory text for "Python Programming". I want to clarify the behaviour of multiply operator (*) when applied to lists. Consider the function make_matrix def make_matrix(rows,…
Aman Aggarwal
  • 3,557
  • 4
  • 22
  • 37
10
votes
1 answer

Setting a class __name__ declaratively

Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier? >>> class Potato: ... __name__ = 'not Potato' ... >>> Potato.__name__ # doesn't stick 'Potato' >>> Potato().__name__ # .. but…
wim
  • 266,989
  • 79
  • 484
  • 630
9
votes
3 answers

Getting object's parent namespace in python?

In python it's possible to use '.' in order to access object's dictionary items. For example: class test( object ) : def __init__( self ) : self.b = 1 def foo( self ) : pass obj = test() a = obj.foo From above example, having 'a'…
grigoryvp
  • 33,993
  • 56
  • 155
  • 263
8
votes
3 answers

Is there any way to tell if a function object was a lambda or a def?

Consider the two functions below: def f1(): return "potato" f2 = lambda: "potato" f2.__name__ = f2.__qualname__ = "f2" Short of introspecting the original source code, is there any way to detect that f1 was a def and f2 was a lambda? >>>…
wim
  • 266,989
  • 79
  • 484
  • 630
7
votes
3 answers

Python Reflection and Type Conversion

In Python, functions like str(), int(), float(), etc. are generally used to perform type conversions. However, these require you to know at development time what type you want to convert to. A subproblem of some Python code I'm trying to write is…
dsimcha
  • 64,236
  • 45
  • 196
  • 319
7
votes
3 answers

Why doesn't Python have a "__req__" (reflected equality) method?

I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: >>> arr = np.array([1,2,3,4,5]) >>>…
acdr
  • 4,130
  • 13
  • 39
7
votes
4 answers

python attribute lookup without any descriptor magic?

I've started to use the python descriptor protocol more extensively in the code I've been writing. Typically, the default python lookup magic is what I want to happen, but sometimes I'm finding I want to get the descriptor object itself instead the…
Matt Anderson
  • 17,538
  • 11
  • 38
  • 55
6
votes
1 answer

When can dict_values views be set-like (and why)?

The docs say that values views are not treated as set-like, but sometimes they are: >>> d = {1: 1} >>> d.values() | d.keys() {1} >>> d.values() & d.keys() {1} >>> d.values() - d.keys() set() Why implement set-returning set semantics but then…
wim
  • 266,989
  • 79
  • 484
  • 630
6
votes
1 answer

Is definition order available in a module namespace?

It's documented that the definition order in classes is preserved (see also PEP 520): If the metaclass has no __prepare__ attribute, then the class namespace is initialised as an empty ordered mapping. Is the definition order also preserved in…
wim
  • 266,989
  • 79
  • 484
  • 630
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,463
  • 9
  • 22