getattr

Why isn't there __setattribute__ in python?

Why isn't there __setattribute__ in python? Question: Python has these methods: __getattr__, __getattribute__, and __setattr__. I know the difference between __getattr__ and __getattribute__, this thread explains it well. But I couldn’t find anywhere anybody mentioning why __setattribute__ doesn’t exist. Does somebody know the reasons for this? Thank you. Asked By: acmpo6ou || Source Answers: When …

Total answers: 1

When using multiprocessing and spawn in python, use self.a in __getattr__ cause infinite loop

When using multiprocessing and spawn in python, use self.a in __getattr__ cause infinite loop Question: The following code will recurrent the bug: from multiprocessing import Process, set_start_method class TestObject: def __init__(self) -> None: self.a = lambda *args: {} def __getattr__(self, item): return self.a class TestProcess(Process): def __init__(self, textobject, **kwargs): super(TestProcess, self).__init__(**kwargs) self.testobject = textobject def …

Total answers: 2

Getting text from a <pre> object with Selenium

Getting text from a <pre> object with Selenium Question: I’m trying to get the text inside of <pre> tag and I have tried with get_attribute(‘text’), get_attribute(‘value’), .text(), .value(), get_attribute("innerHTML") but I keep failing: Snapshot: This is the code that i’m using: import unittest import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui …

Total answers: 1

AttributeErrors: undesired interaction between @property and __getattr__

AttributeErrors: undesired interaction between @property and __getattr__ Question: I have a problem with AttributeErrors raised in a @property in combination with __getattr__() in python: Example code: >>> def deeply_nested_factory_fn(): … a = 2 … return a.invalid_attr … >>> class Test(object): … def __getattr__(self, name): … if name == ‘abc’: … return ‘abc’ … raise AttributeError(“‘Test’ …

Total answers: 3

How to get foreign key values with getattr from models

How to get foreign key values with getattr from models Question: I have a model Project and i am getting the attributes of that with the following instr attr = getattr(project, ‘id’, None) project is the instance, id is the field and None is the default return type. My question is: what if I want …

Total answers: 2

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

strange behavior with lamba: getattr(obj, x) inside a list

strange behavior with lamba: getattr(obj, x) inside a list Question: In the following example: class A(object): pass prop1 = 1 prop2 = 2 prop3 = 3 prop4 = 4 obj = A() tmp = [‘prop1’, ‘prop2’, ‘prop3’, ‘prop4’] getter = [ lambda: getattr(obj, x) for x in tmp ] I am always getting 4 when …

Total answers: 1

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

getattr() versus __dict__ lookup, which is faster?

getattr() versus __dict__ lookup, which is faster? Question: I can dynamically look up object attribute values using object.__dict__[some_key] or getattr(object, some_key). Which is faster or better, and why? >>> class SomeObject: … pass … >>> so = SomeObject() >>> so.name = ‘an_object’ >>> getattr(so,’name’) ‘an_object’ >>> so.__dict__[‘name’] ‘an_object’ Asked By: Cole || Source Answers: You …

Total answers: 1