Questions tagged [getattr]

getattr is a Python built-in function used to access a named attribute on an object.

The function's schematics are as follows:

getattr(object:object, name:str[, default:object]) -> value

where object is the object, name is the named attribute, and default, if supplied, is the default value to return if the attribute can not be found. If default is not supplied and the object can not be found, an AttributeError is thrown.

Below is a demonstration of the function's features:

>>> class Test:
...     def __init__(self):
...         self.attr = 1
...
>>> myTest = Test()
>>> getattr(myTest, 'attr')
1
>>> getattr(myTest, 'attr2', 'No attribute by that name')
'No attribute by that name'
>>> getattr(myTest, 'attr2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Test instance has no attribute 'attr2'
>>>
378 questions
2
votes
0 answers

setattribute for classvariable

I am trying to set a class attribute basically redisconnection object once the connection is established to a variable in another class. I use setattr to set the variable. When i try to access the variable the value is null. import importlib import…
Balachandar
  • 1,538
  • 3
  • 16
  • 25
2
votes
2 answers

getattr with two tuples after it

I am working with a code base which contains a line which I really can't understand: x, x, z = getattr(ReceiveFile, maxsizes)(input, args) So if it didn't have the second tuple at the end it would be just x, y, z = ReceiveFile.maxsizes How do I…
cardamom
  • 6,873
  • 11
  • 48
  • 102
2
votes
1 answer

How to multithread with getattr, one thread per property?

Suppose I have the following object with multiple expensive properties, as so: class Object: def __init__(self, num): self.num = num @property def expensive_property(self): return expensive_calculation @property def…
callmeGuy
  • 944
  • 2
  • 11
  • 28
2
votes
1 answer

the fundamental differences of the way to overwrite getattr and setattr

My hope is to make attributes case-insensitive. But overwriting __getattr__ and __setattr__ are somewhat different, as indicated by the following toy example: class A(object): x = 10 def __getattr__(self, attribute): return…
J. Lin
  • 139
  • 3
  • 11
2
votes
3 answers

method __getattr__ is not inherited from parent class

Trying to subclass mechanize.Browser class: from mechanize import Browser class LLManager(Browser, object): IS_AUTHORIZED = False def __init__(self, login = "", passw = "", *args, **kwargs): super(LLManager, self).__init__(*args,…
Крайст
  • 776
  • 1
  • 9
  • 22
2
votes
1 answer

Combining __setattr__ and __getattr__ causes infinite loop

I'm trying to build a system where a base class is used for every other object. Every base object has a _fields dictionary internally where implementations of the base class can store their information. Base class implementation is quite…
Yonathan
  • 1,253
  • 1
  • 17
  • 29
2
votes
1 answer

use a variable to return a certain instance variable

I'm working in Python and in a class method, I am trying to return an instance variable, but depending on the variable I want to return another instance variable: def someFunction(self, variable): return self.variable This is what I am trying…
Noortje
  • 21
  • 1
2
votes
1 answer

Using __getattr__ in Python to update an attribute?

I am trying to modify an attribute that is defined in a class. For this problem, I had to modify the names of the attributes to include private_ in the beginning. So, if it was previously self.a, it is now self.private_a in self.__dict__. However,…
hannahtran
  • 21
  • 4
2
votes
1 answer

Python recursion on _getattr_ in my class decorator

I write a function (Python 3.x) for use it as a class decorator than make attributes private or public. First of all I wrote my 'private' function: def private(attrlist): def wrapper(obj): class fPrivate: def…
AleMal
  • 1,977
  • 6
  • 24
  • 49
2
votes
1 answer

Python: Import function from file whose name is a variable

I have a file ./model_scripts/medians.py containing a function predict_volume(). [Output of tree in the bash terminal given below] model_scripts ├── __init__.py ├── medians.py └── ... I need to import this function into another python script…
IM94
  • 153
  • 1
  • 12
2
votes
1 answer

Python deepcopy with custom __getattr__ and __setattr__

I have implemented a class that can distinguish between a fixed set of instance attributes (let's call them meta-attributes) and an arbitrary set of other instance attributes. It has custom __getattr__ and __setattr__: class MyClass(object): …
spiderface
  • 1,025
  • 2
  • 11
  • 16
2
votes
3 answers

can't access an attribute's attribute w getattr?

Based on In [65]: %paste class Thing(object): def __init__(self, obj): self.legs = 4 self.obj = obj def length(self): return 6 class Thing2(object): def __init__(self): self.arms = 2 ## -- End pasted…
codyc4321
  • 9,014
  • 22
  • 92
  • 165
2
votes
2 answers

use one function to getattr for either functions or regular attributes

I have the following code: In [38]: %paste def set_session_attribute(obj, attribute): if attribute.endswith('()'): attribute = attribute.replace('()', '') return getattr(obj, attribute)() else: return getattr(obj,…
codyc4321
  • 9,014
  • 22
  • 92
  • 165
2
votes
1 answer

Django - Use getattr() to retrieve Model Method (@property)

I would like to do this: def retrieve_data(self, variable): start_date = datetime.date.today() - datetime.timedelta(int(self.kwargs['days'])) invoice_set = InvoiceRecord.objects.filter(sale_date__gte=start_date) total = 0 for invoice…
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
2
votes
2 answers

Why there is no infinite loop while overriding getattr method in python

I am trying to override getattr method and as per my understanding there should be infinite loop in the following code snippet as by default object.__getattribute__(self,attr) is invoked which will invoke overrided getattr method as attribute…
Vinit89
  • 583
  • 1
  • 7
  • 31