setattr

Block setting of class attributes (__setattr__)

Block setting of class attributes (__setattr__) Question: 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 __setattr__(self, key, value): raise PermissionError(‘in base’) def __init_subclass__(cls, *args, **kwargs): def _setattr_(inst, key, val): raise PermissionError(‘in derived’) cls.__setattr__ = …

Total answers: 2

Automatically add decorator to all inherited methods

Automatically add decorator to all inherited methods Question: 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 manually re-declare (override) the inherited methods in B but instead, automatically …

Total answers: 2

Dynamically creating read-only attributes

Dynamically creating read-only attributes Question: I’m currently trying to create a class that inherits from set, but allows calling for subsets with attributes. Since I want to create this class such that I can use it in any context, I want to have it be able to create attributes from any given string. I have …

Total answers: 1

getattr and setattr on nested subobjects / chained properties?

getattr and setattr on nested subobjects / chained properties? Question: I have an object (Person) that has multiple subobjects (Pet, Residence) as properties. I want to be able to dynamically set the properties of these subobjects like so: class Person(object): def __init__(self): self.pet = Pet() self.residence = Residence() class Pet(object): def __init__(self,name=’Fido’,species=’Dog’): self.name = name …

Total answers: 12

Switchable dot access to Python dicts?

Switchable dot access to Python dicts? Question: I haven’t seen a toggleable version of dictionary dot-access yet. My first-pass attempt here doesn’t work: class DottableDict (dict): def allowDotting (self, state=True): if state: self.__setattr__ = dict.__setitem__ self.__getattr__ = dict.__getitem__ else: del self.__setattr__ del self.__getattr__ >>> import dot >>> d = dot.DottableDict() >>> d.allowDotting() >>> d.foo = …

Total answers: 1

How can I dynamically create class methods for a class in python

How can I dynamically create class methods for a class in python Question: If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, ‘func’, classmethod(self._func)) if __name__ == "__main__": a.func I receive …

Total answers: 6

How to use __setattr__ correctly, avoiding infinite recursion

How to use __setattr__ correctly, avoiding infinite recursion Question: I want to define a class containing read and write methods, which can be called as follows: instance.read instance.write instance.device.read instance.device.write To not use interlaced classes, my idea was to overwrite the __getattr__ and __setattr__ methods and to check, if the given name is device to …

Total answers: 6

Difference between setattr and object manipulation in python/django

Difference between setattr and object manipulation in python/django Question: I have the following model: class Ticket(models.Model): title = models.CharField() merged_to = models.ForeignKey(“self”, related_name=’merger_ticket’, null=True, blank=True) looser_ticket = models.BooleanField(default=False) There are couple of ways of manipulating the model: First ticket = Ticket.objects.get(pk=1) ticket.title = “This is edit title” ticket.merged_to_id = 2 ticket.looser_ticket = True Second ticket …

Total answers: 2

Using setattr() in python

Using setattr() in python Question: I am looking for someone to explain the basics of how to use, and not use setattr(). My problem arose trying to use one class method/function to return data that is then put in another method/function. Perhaps a simpler approach would be much better in this case, but I’m trying …

Total answers: 6