201 questions
1
vote
1
answer
142
views
How to implement Python's unified attribute lookup (__getattribute__) in C++, handling descriptors and metaclasses?
I'm developing a Python interpreter from scratch in C++ as a hobby project to deepen my understanding of the language's internals.
I'm currently stuck on implementing the attribute lookup mechanism (...
1
vote
1
answer
76
views
Exception thrown in python magic method is lost on the way
For a project, I want to prevent the use of certain methods in an overriding class.
As this happens rather frequently, I am using a metaclass to block many methods at once (how and why is not within ...
1
vote
1
answer
128
views
How to create different type for class variable and instance variable
I want to explain to Pyright that my variables in class and instance have different types.
I managed to overload __get__ method to achieve this, but now Pyright complains about initialization of ...
1
vote
1
answer
81
views
How to check if an object is a method_descriptor
Given an object, how can I make a check if it is a method_descriptor?
is_method_descriptor = isinstance(obj, method_descriptor) # does not work
The problem: method_descriptor is a builtin but not an ...
1
vote
1
answer
74
views
Dynamic read-only attributes in python instances
EDIT This code contains several bugs, see jsbueno's answer below for a correct version
I would like to create read-only attributes that dynamically retrieve values from an internal dictionary. I have ...
2
votes
3
answers
196
views
Properties vs. generic setter/getter and descriptor
I can't seem to find a definitive answer on the matter and I guess the reason is because it depends on the situation.
a, b and c (and d, e, f... as only 3 attributes are listed in this example for ...
0
votes
0
answers
43
views
How to track access to class variables in a custom ORM using Python descriptors?
I'm building my own ORM in Python and ran into an issue. I want to track the access to class variables in order to know which ForeignKey is being used in each part of my code.
class IQuery(ABC):
&...
1
vote
1
answer
56
views
Decorate instance methods with decorator class that replaces method with class instance
I would like to be able to decorate instance methods with a class Step such that the methods are replaced by a Step object. At the same time, I'd like to have the option be able to instantiate a step ...
2
votes
0
answers
231
views
Type hinting a python descriptor implementing read-only instance attributes after initialisation
I have a couple of data structure classes that are intended to have public read-only attributes. I cannot use dataclasses or namedtuples as I need the ability to define args, kwargs in the child ...
-1
votes
1
answer
79
views
Is there a better way of detecting when attributes are changed than using decorators?
I have been refactoring a text class meant for blitting text to the screen.
I recently learned about the @property decorator and thought using it in this class would help.
Here is a stripped down ...
3
votes
3
answers
118
views
Accessing attributes of a Python Descriptor
Not sure if this is feasible or not. The implementation/example below is dummy, FYI.
I have a Python class, Person. Each person has a public first name and a public last name attribute with ...
1
vote
1
answer
61
views
How to get a variable name from within the function it call for assignment? [duplicate]
I need to get the name of a variable from within a function it call during assignment. For example if we take the code:
class CharField:
def __init__(self):
self.field_name = ???
...
0
votes
1
answer
103
views
Mechanics of python attribute setting on class types, for use in descriptor setters
Recently, with the change of the @classmethod decorator no longer being able to wrap the @property decorator (Python >= 3.11), there has been significant interest in how to create a @classproperty ...
0
votes
0
answers
55
views
Why does this python decorator refuse to set the setter?
This is my code:
class Property:
def __init__(self, fget, fset):
self.fget = fget
self.fset = fset
def __get__(self, obj, objtype=None):
return self.fget(obj)
...
-1
votes
1
answer
88
views
Automatic updates for class attributes when modifying a list-like attribute in Python using __set__ and __setitem__
I have a class with an attribute ax_mask. The attribute is list like. When I change the whole attribute or just an element of it, I need some other attribute (window) in the class to be updated (i.e. ...
0
votes
0
answers
43
views
Generic XML message parsing
I have a number of XML message types with an elaborate header and sequence structure, but separate business message types. I'm trying convert these to objects in Python, and so far my bottom-level ...
4
votes
1
answer
113
views
Let a passed function object be called as a method
EDIT: I extended the example to show the more complex case that I was talking about before. Thanks for the feedback in the comments.
Some more context:
I am mostly interested in this on a theoretical ...
3
votes
2
answers
709
views
Using a cached property on a named tuple
from typing import NamedTuple
from functools import cached_property
class Rectangle(NamedTuple):
x: int
y: int
@cached_property
def area(self):
return self.x * self.y
I ...
3
votes
1
answer
129
views
Why does property override object.__getattribute__?
I noticed that contrary to the classmethod and staticmethod decorators, the property decorator overrides the object.__getattribute__ method:
>>> list(vars(classmethod))
['__new__', '__repr__',...
2
votes
3
answers
181
views
Descriptor's __set__ not invoked
I have a innerclass decorator/descriptor that is supposed to pass the outer instance to the inner callable as the first argument:
from functools import partial
class innerclass:
def __init__(self, ...
1
vote
2
answers
129
views
Keeping multiple dictionaries in sync when one is the super set of the rest
I want to have a class with dictionaries that contain nodes, but nodes can be of several sorts: points, vertices,... (and possible others)
I want to efficiently loop and access over either one of ...
0
votes
1
answer
113
views
Why cls.__getattribute__ returns a function instead of a bound method whereas cls.__call__ returns a bound method
Suppose cls is a class in python. cls.__getattribute__ returns the __getattribute__ function defined on object, but it doesn't bind cls to this function. According to the descriptor protocol, cls is ...
5
votes
3
answers
188
views
Descriptors in python for implementing perl's tie scalar operation
I need some help with descriptors in python. I wrote an automatic translator from perl to python (Pythonizer) and I'm trying to implement tied scalars, which is basically an object that acts as a ...
0
votes
0
answers
49
views
Why does function descriptor create new bound method each time
Could you explain why new bound method is created each time when trying to access same method of the same class instance?
class MyClass:
def my_method(self):
print(f"Called bounded to ...
0
votes
2
answers
320
views
Why does super().__dict__ raise an AttributeError?
Why is this raising an AttributeError?
class A:
def f(self):
print(super().__dict__)
A().f() # raises AttributeError: 'super' object has no attribute '__dict__'