Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
151 views

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 (...
Antonio Sidnei's user avatar
2 votes
3 answers
197 views

classonlymethod decorator/descriptor defined below is like the built-in classmethod, but you're not allowed to call the method on instances, only on the class itself. from typing import Concatenate, ...
yuri kilochek's user avatar
1 vote
1 answer
77 views

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 ...
502E532E's user avatar
  • 599
1 vote
0 answers
72 views

I wrote a simple code where I override the __getattribute__ method so that it always returns an empty string. But Pyright doesn't understand this and complains "int" is not assignable to &...
Alex Filiov's user avatar
1 vote
1 answer
129 views

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 ...
Alex Filiov's user avatar
2 votes
1 answer
75 views

Passing a class, or a totally different object, as the first argument to method is easy: class Foo: def method(self): ... Foo.method(object()) # pass anything to self I wonder, is this possible ...
Daraan's user avatar
  • 5,165
1 vote
1 answer
85 views

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 ...
Daraan's user avatar
  • 5,165
1 vote
1 answer
91 views

I want to refactor a big part of my code into a generic descriptor for read only attribute access. The following is an example of property based implementation class A: def __init__(self, n): ...
Yehui He's user avatar
1 vote
1 answer
79 views

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 ...
peich's user avatar
  • 33
1 vote
0 answers
170 views

I'm trying to use type hints in my code but I simply don't understand how the concept applies to a Python descriptor. The only relevant info I could find on the matter was this 6+ year old post but I ...
YoRHa_A2's user avatar
2 votes
3 answers
197 views

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 ...
YoRHa_A2's user avatar
0 votes
0 answers
16 views

I want to create a @property like feature with serialization capabilities. It would take some arguments. I have created a descriptor like so: class CustomMethod: def __init__(self, serialize: bool ...
FTG's user avatar
  • 75
0 votes
0 answers
47 views

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): &...
phzamora's user avatar
0 votes
2 answers
92 views

Here is descriptor class: #descriptor class Validator: def __set_name__(self,owner,name): self.private_name = '_' + name def __set__(self,obj,value): self.validate(value) ...
Subham Kumar's user avatar
1 vote
1 answer
60 views

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 ...
mauro's user avatar
  • 11
1 vote
1 answer
189 views

I want to create a decorator that adds a member to the decorated class that is instance of outer class. In this case, decorator memberclass adds attribute self to the instance of class y that is of ...
Superior's user avatar
  • 885
2 votes
0 answers
237 views

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 ...
Parikshit's user avatar
0 votes
1 answer
167 views

I have made a custom descriptor which I've assigned to a value in my class. However I would like to set the instance variable initially without using the descriptors __set__ method. class Descriptor: ...
r5ne's user avatar
  • 13
-1 votes
1 answer
80 views

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 ...
r5ne's user avatar
  • 13
1 vote
2 answers
139 views

I have the following code involving a decorator decorating a property getter into a property-like object that reimplements mutation dunders to also invoke a specified function attribute fetched from ...
user26109373's user avatar
3 votes
3 answers
127 views

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 ...
pemm's user avatar
  • 302
0 votes
1 answer
55 views

I was trying to play around with Python descriptors and Pycharm IDE seems to be complaining Am I using descriptors incorrectly? Or is this expected? Complete code: class Age: def __set__(self, ...
Aniket Thakur's user avatar
1 vote
1 answer
63 views

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 = ??? ...
Carl L's user avatar
  • 53
1 vote
1 answer
107 views

I have a data-descriptor whose purpose is to read and write data from file, and delete this file : import os class ValueFromFile: ... def __set_name__(self, owner, name): self.name =...
Vincent's user avatar
  • 89
0 votes
1 answer
106 views

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 ...
Mr G's user avatar
  • 268
0 votes
1 answer
93 views

I'm working on a class for which user should be able to set its fields in the most convenient way, which includes assigning strings to any of the fields. Values assigned by the user should be ...
Michał Góral's user avatar
0 votes
0 answers
55 views

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) ...
forstack overflowizi's user avatar
0 votes
1 answer
67 views

I was reading about python descriptors and how to use them. I came across an example from the python official doc, import logging logging.basicConfig(level=logging.INFO) class LoggedAgeAccess: ...
Roviostar's user avatar
-1 votes
1 answer
88 views

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. ...
fuhrmanj's user avatar
2 votes
1 answer
117 views

The Descriptor HowTo Guide (Descriptor protocol) specifies that __get__() must be callable with 2 and 3 arguments: descr.__get__(self, obj, type=None) -> value Why has it been defined like this? ...
Feuermurmel's user avatar
  • 10.1k
0 votes
0 answers
43 views

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 ...
Woody1193's user avatar
  • 8,202
6 votes
2 answers
580 views

I stumbled upon this code that I found weird as it seems to violate the fact that python builtins call dunder methods directly from the class of the object. Using __call__ as an example, if we define ...
Zhang Daniel's user avatar
4 votes
1 answer
114 views

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 ...
Alexander's user avatar
  • 394
2 votes
1 answer
185 views

I've started reading about descriptors in Python and created small test to see whether the order of attribute access is same as it is described. As I understand when attribute of object is accessed it ...
Maciej Sinicki's user avatar
3 votes
2 answers
727 views

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 ...
COVFEFE-19's user avatar
1 vote
4 answers
727 views

Earlier today i stumbled upon a behaviour with python classes which i could not fully comprehend. If we take this piece of code: class A: a: str When I test out the attribute access for a like so ...
nosahama's user avatar
  • 160
3 votes
1 answer
133 views

I noticed that contrary to the classmethod and staticmethod decorators, the property decorator overrides the object.__getattribute__ method: >>> list(vars(classmethod)) ['__new__', '__repr__',...
Géry Ogam's user avatar
  • 8,434
1 vote
1 answer
169 views

UPDATED Question: In Python, can I create a custom data structure that is a dict, but which I get and set as a set, and for which I can create a custom __str__ representation? I want a class attribute ...
yetixhunting's user avatar
2 votes
3 answers
188 views

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, ...
InSync's user avatar
  • 12.2k
50 votes
3 answers
24k views

In Python 3.9, we gained the ability to chain @classmethod and @property to sensibly create class properties. class Foo: @property def instance_property(self): return "A regular property&...
kg583's user avatar
  • 876
1 vote
2 answers
131 views

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 ...
Jaka Belec's user avatar
3 votes
1 answer
872 views

Suppose we have the following classes: class Foo: def __init__(self, method): self.method = method def __get__(self, instance, owner): if instance is None: return self ...
feiyang472's user avatar
0 votes
1 answer
113 views

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 ...
Zhang Daniel's user avatar
2 votes
1 answer
189 views

I have a following (simplified of course) descriptor: class d: def __init__(self, method): self.method = method def __get__(self, instance, owner=None): print(instance, owner, ...
wRAR's user avatar
  • 25.7k
5 votes
3 answers
190 views

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 ...
snoopyjc's user avatar
  • 633
0 votes
2 answers
56 views

Is there a way to access a class (where function is defined as a method) before there is an instance of that class? class MyClass: def method(self): print("Calling me") m1 = ...
GopherM's user avatar
  • 720
0 votes
0 answers
49 views

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 ...
GopherM's user avatar
  • 720
0 votes
1 answer
521 views

Is it possible to assign property on a function object the similar way we assign it on class instances. My desired behaviour is like this def prop(): print("I am a property") def ...
GopherM's user avatar
  • 720
0 votes
2 answers
335 views

Why is this raising an AttributeError? class A: def f(self): print(super().__dict__) A().f() # raises AttributeError: 'super' object has no attribute '__dict__'
zwithouta's user avatar
  • 1,611
1 vote
2 answers
196 views

I have a decorator that I can use to mark a read-only class property: class class_ro_property(property): def __init__(self, getter:Callable): self._getter = getter def __get__(self, _,...
Tom's user avatar
  • 8,181

1
2 3 4 5