getattr Questions

1

I am curious if there is a way in Python 3 to type hint the return type of __getattr__() method in Python. I have the following dummy code I was using for test purposes: class MyClass: def __geta...
Freeze asked 30/5, 2022 at 13:56

9

Solved

How can implement the equivalent of a __getattr__ on a class, on a module? Example When calling a function that does not exist in a module's statically defined attributes, I wish to create an ins...
Entopic asked 15/3, 2010 at 13:20

15

Solved

I read an article about the getattr function, but I still can't understand what it's for. The only thing I understand about getattr() is that getattr(li, "pop") is the same as calling li....
Cyprio asked 2/11, 2010 at 5:45

2

Solved

Consider the following code example (python 2.7): class Parent: def __init__(self, child): self.child = child def __getattr__(self, attr): print("Calling __getattr__: "+attr) if hasattr(self...
Daystar asked 8/12, 2012 at 10:12

3

Solved

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_...
Christmastide asked 12/4, 2016 at 13:39

2

Solved

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 retur...
Wivern asked 27/11, 2013 at 6:54

4

Solved

Consider this code: class Foo1(dict): def __getattr__(self, key): return self[key] def __setattr__(self, key, value): self[key] = value class Foo2(dict): __getattr__ = dict.__getitem__ __seta...
Prude asked 10/6, 2011 at 10:47

3

Solved

class A: __slots__ = ("a",) def __init__(self) -> None: self.a = 1 class B1: __slots__ = ("b",) def __init__(self, b) -> None: self.b = b def __getattr__(self, k)...
Metagalaxy asked 10/8, 2020 at 10:44

5

Solved

I have an object with nested attributes like so: obj: attr_1: attr_2 When both attr_1 and attr_2 exists, I can get it like so: obj.attr_1.attr_2 But what if I'm not sure if either attribute exi...
Inbred asked 5/3, 2017 at 8:34

3

I have a problem in python. I have a class whis custom __getattr__ class ChoiceNumToName(object): def __init__(self, django_choice_tuple): self.ods_choice_tuple = django_choice_tuple self.choi...
Rotarian asked 15/11, 2017 at 4:11

3

Solved

I want to call some unknown function with adding parameters using getattr function. Is it possible?
Context asked 12/6, 2011 at 12:30

3

Solved

Is there any way to determine the difference between a method and an attribute call using __getattr__? I.e. in: class Bar(object): def __getattr__(self, name): if THIS_IS_A_METHOD_CALL: # Hand...
Gosser asked 21/11, 2013 at 12:26

8

Solved

I am trying to understand when to define __getattr__ or __getattribute__. The python documentation mentions __getattribute__ applies to new-style classes. What are new-style classes?
Molliemollify asked 19/7, 2010 at 2:11

1

Solved

I am trying to understand a piece of Python code. First, I have the following file structure: folder1-- model __init__.py file1.py file2.py __init__.py The __init__.py in the folder folder1 h...
Anemography asked 31/7, 2020 at 0:27

5

Solved

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, m...
Dooley asked 10/6, 2013 at 8:53

3

Solved

I'm using getattr to call different functions depending on a variable. Im doing something like that: getattr(foo, bar) () That works, calling functions like foo.bar() My problem is that I have...
Becnel asked 2/8, 2012 at 15:51

4

Solved

I am trying to understand the difference between __getattr__ and __getattribute__, however, I am failing at it. The answer to the Stack Overflow question Difference between __getattr__ vs __getatt...
Transpire asked 28/11, 2010 at 6:23

5

Solved

I have a class like: class MyClass: Foo = 1 Bar = 2 Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python?...
Flite asked 1/7, 2010 at 6:9

1

Solved

How can I modify the classes below to make them pickeable? This question: How to make a class which has __getattr__ properly pickable? is similar but refer to wrong exception in the use of getatt...
Speller asked 20/3, 2018 at 9:15

4

Solved

What do I pass as the first parameter "object" to the function setattr(object, name, value), to set variables on the current module? For example: setattr(object, "SOME_CONSTANT", 42); giving th...
Navarra asked 29/5, 2010 at 2:7

3

Solved

I would like to make bool binary operations using the magic methods for these operators. For example, I can get a < b as getattr(a, '__lt__')(b) or a == b as getattr(a, '__eq__')(b). Can I get...
Fruit asked 5/12, 2016 at 18:17

1

Solved

I read a bit on Python's object attribute lookup: Seems pretty straight forward, so I tried it out (python3): class A: def __getattr__(self, attr): return (1,2,3) a = A() a.foobar #returns (1,2...
Latoyialatreece asked 19/8, 2016 at 16:35

1

Solved

I dont know if this is something an expected behavior of getattr built_in method. getattr executes the default(3rd) argument as well even if the actual argument(2nd) satisfies the condition. Exampl...
Saxen asked 26/7, 2016 at 12:23

1

Solved

I have the following class: class MyInt: def __init__(self, v): if type(v) != int: raise ValueError('value must be an int') self.v = v def __getattr__(self, attr): return getattr(self.v, at...
Cornea asked 10/7, 2016 at 17:59

2

Solved

Given the following class (with a buggy property) then what is the best foolproof way of checking that the bar property exists? class Foo(object): @property def bar(self): raise AttributeError(...
Reflexion asked 19/5, 2016 at 8:0

© 2022 - 2024 — McMap. All rights reserved.