208 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
1
vote
1
answer
89
views
`getattr`, `setattr` and `delattr` restricted to byte-compiled code
I was going through classes doc. And I found the following paragraph.
Notice that code passed to exec() or eval() does not consider the classname of the invoking class to be the current class; this ...
0
votes
1
answer
63
views
Using `setattr` to decorate functions from another module
I'm having trouble applying a decorator to an imported function. Suppose I have the following foo.py module:
# contents of foo.py
def bar():
return "hello"
I now want to import bar from ...
1
vote
1
answer
248
views
How to avoid `setattr` (and `getattr`) when using Python? And is it necessary to avoid
If I want to add a value to a field in a protocol buffer that isn't known at compile time, I'm currently doing setattr. I normally don't like using setattr because it seems less secure. But when I ...
1
vote
1
answer
58
views
Python: trying to access instance attributes in self.__setattr__
I was trying to create a dataclass with shorter aliases for longer fields. Since I wanted to be able to add new fields (with long and short names) without having to write a @property decorated "...
0
votes
0
answers
103
views
setattr doesn't seem to have an effect when setting multi-level attributes
I'm writing a mod for a Ren'Py game that includes possibility to edit game's variables' values ("cheating"). Variables in the game include different ones from simple booleans to pretty ...
-3
votes
1
answer
44
views
Why __getattr__ triggers __setattr__ in the following python code? [closed]
class D:
def __init__(self):
self._attr = 1
self._attr2 = 2
def __getattr__(self, name):
if name == 'data':
return (self._attr, self._attr2)
def ...
1
vote
2
answers
88
views
setattr for `__call__` method of an object doesn't work in Python [duplicate]
I though that for python object obj() is equivalent to obj.__call__(). But it looks like it doesn't hold when setattr was used.
In [46]: class A:
...: def __call__(self):
...: ...
0
votes
0
answers
180
views
How to add custom attributes to a Python list?
I'm trying to associate additional data with a Python list. Ideally, I'd like to add custom attributes to a list object, similar to how you'd add attributes to a custom class. For example:
a = [1, 2, ...
0
votes
0
answers
27
views
Does setattr make a deepcopy? [duplicate]
https://docs.python.org/3/library/functions.html#setattr From this documentation, I cannot tell if a deep copy is made or not.
I'm working with a codebase currently that uses setattr is a rather ...
3
votes
1
answer
77
views
Python classes, difference between setting an attribute and using __setattr__()
I'm trying to set attributes to a class of which I don't know the name a-priori. I also want to avoid users to write to that attribute, so I use a property factory with getters and setters which ...
0
votes
0
answers
95
views
How to set attribute with index in the name in python
I'm trying to setattr() using name list. When calling it the names containing index number in square brackets raises AttributeError in Python 2 and TypeError in Python.
class NodeClass():
def __init__(...
0
votes
0
answers
103
views
Excluding an attribute from __setattr__ and __getattr__ in Python?
I'm trying to implement a Config class that holds and keeps track of an arbitrary amount of Settings. I'd like to get and set the Settings like c = Config(<CONTAINER_OF_POSSIBLE_SETTINGS>); c.x =...
0
votes
0
answers
89
views
AttributeError and RecursionError happened when using `__setattr__` in python
I used super() as a part of the __setattr__ method to update a dictionary whenever an attribute value is set on an instance of a class; however, a warning reported as my class object has no such ...
0
votes
2
answers
139
views
Block setting of class attributes (__setattr__)
is there a simple way to prevent setting new class attrs?
while trying with the following snippet, shouldn't setattr(Derived, "test1", 1) call the __setattr__ from Base?
class Base:
def ...
3
votes
2
answers
827
views
Automatically add decorator to all inherited methods
I want in class B to automatically add the decorator _preCheck to all methods that have been inherited from class A. In the example b.double(5) is correctly called with the wrapper. I want to avoid to ...