python-datamodel

Are there any unique features provided only by metaclasses in Python?

Are there any unique features provided only by metaclasses in Python? Question: I have read answers for this question: What are metaclasses in Python? and this question: In Python, when should I use a meta class? and skimmed through documentation: Data model. It is very possible I missed something, and I would like to clarify: …

Total answers: 1

List of all Python dunder/magic methods – Which ones do you need to implement to correctly proxy an object?

List of all Python dunder/magic methods – Which ones do you need to implement to correctly proxy an object? Question: 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 __len__, __getitem__, …

Total answers: 1

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

Why doesn't Python have a "__req__" (reflected equality) method? Question: 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]) >>> arr == AnyOf(2,3) np.array([False, True, True, False, False]) without having to use …

Total answers: 3

What is the relationship between the Python data model and built-in functions?

What is the relationship between the Python data model and built-in functions? Question: 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 …

Total answers: 2

Schrödinger's variable: the __class__ cell magically appears if you're checking for its presence?

Schrödinger's variable: the __class__ cell magically appears if you're checking for its presence? Question: There’s a surprise here: >>> class B: … print(locals()) … def foo(self): … print(locals()) … print(__class__ in locals().values()) … {‘__module__’: ‘__main__’, ‘__qualname__’: ‘B’} >>> B().foo() {‘__class__’: <class ‘__main__.B’>, ‘self’: <__main__.B object at 0x7fffe916b4a8>} True It seems like the mere mention of …

Total answers: 2

Prevent creating new attributes outside __init__

Prevent creating new attributes outside __init__ Question: 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 __setattr__ method such as def __setattr__(self, attribute, …

Total answers: 13

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

When where and how can i change the __class__ attr of an object? Question: I’d like to be able to do: >>> class a(str): … pass … >>> b = a() >>> b.__class__ = str Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: __class__ assignment: only for heap types Asked By: …

Total answers: 4

How is the 'is' keyword implemented in Python?

How is the 'is' keyword implemented in Python? Question: … 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’ …

Total answers: 11

Implementing slicing in __getitem__

Implementing slicing in __getitem__ Question: 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 raises an error about not …

Total answers: 5