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
0
votes
1 answer

Avoid Pycharm __dict__ lookup when using __getattr__ and __slots__ for composition

Say I have a class: class Example: __slots__ = ("_attrs", "other_value") def __init__(self): self._attrs = OrderedDict() self.other_value = 1 self.attribute = 0 def __setattr__(self, key, value): if…
Alesi Rowland
  • 379
  • 2
  • 16
0
votes
1 answer

Could someone please explain how the function getattr(Object, function) works when passed variables?

So I am working on some hackerrank questions and someone posted the following snippet of code as part of a solution and I was curious if someone could explain it to me please? getattr(l, parts[0])(*(map(int, parts[1:]))) In the following…
0
votes
1 answer

Kivy ScreenManager can't reference class from .py file class any more. Attribute error: 'super' has

I just started developing my first app in python using kivy. I generated the general layout of the app and also some logic in an python file in addition to my main py to seperate everything a bit. This worked perfectly fine and the app was running…
0
votes
1 answer

Can I dynamically choose the method applied on a pandas Resampler object?

I am trying to create a function which resamples time series data in pandas. I would like to have the option to specify the type of aggregation that occurs depending on what type of data I am sending through (i.e. for some data, taking the sum of…
Tom
  • 8,310
  • 2
  • 16
  • 36
0
votes
1 answer

python zerorpc client with multiprocess to send log to server

As the title, I want a log rpc server to help me deal with the log in a new process. I reference zerorpc and this link: https://stackoverflow.com/a/57140017/14021161 But stuck in a weird bug The code: import zerorpc from loguru import logger import…
RayChang
  • 13
  • 3
0
votes
1 answer

applying reflection in python to indirectly call objects from another class

I'm trying to make a class like Proxy that I indirectly access all methods of my objects, for example like: class Radio(): def __init__(self): self._channel = "channel" def get_channel(self): …
0
votes
1 answer

python condition that checks if attriibute is not none errors that it is none

I have the following condition that checks to see if an object has populated value for the id attribute: if getattr(instance, 'id', True): asdf = instance.id This should filter any instances that do not have an ID. However I get…
Atma
  • 29,141
  • 56
  • 198
  • 299
0
votes
1 answer

Python class declare by passing variables

I try to create an object by passing the variables, but it seems not work. I put a simple example below to show what I want. Please help me to deal with this issue. Successfully temp = catalog.TEST temp = catalog.PROD Not works, it pass string "i"…
carterlin
  • 15
  • 6
0
votes
0 answers

How to getattr() of a function defined inside another function? Python

I have this file with unit tests: #file tests.py class TestResults(TestBase): ... class TestRegisteredFunctions(TestBase): """Tests functions registered dynamically.""" def testNewFunction(self): """Server must register new…
walkman
  • 478
  • 1
  • 8
  • 21
0
votes
1 answer

In VBA Getattr function with long file path file name greater than 259

I try to list specific file name from directory. but I got error code 53 with Getattr function. In my case, error occurred when file path & file name length is greater than 259 in same directory. Question is how much length does Getattr function…
0
votes
2 answers

trouble in a piece of code python getattr()

I was reading a book Programming Python (4th edition) and I am stuck at a problem with a piece of code including getattr. It is a loop for finding an icon file but I can't figure where it is searching (in what order). I have basic knowledge of using…
0
votes
3 answers

How to access a list element in a python object with getattr?

In python3 I have the following complete code: class Test: def __init__(self): self.x = [42, 4711] self.y = [4, 6] self.z = 99 t = Test() v = 'x[1]' # or can be 'y[0]' or can be 'z'…
Alex
  • 41,580
  • 88
  • 260
  • 469
0
votes
2 answers

Turn string into object for getattr

I'm learning Python with turtle graphics. What I need is to call a movement using a name given by the user and the action so far I have been trying to use: def mov(): selection = action =
0
votes
0 answers

getattr(module_name, class_name) differences between python 2 and 3

I am porting code from Python 2 to 3. The Python 2 version uses this snippet to instantiate a class successfully: #at this point, "module_name" is just a string module = __import__(module_name, globals(), locals(), []) #class_name is just a…
Bee
  • 195
  • 1
  • 3
  • 12
0
votes
1 answer

Making buttons by cycle with lambda and getattr

I'm trying to use getattr inside lambda and I`m getting error: is it possible to do like this or not? This code works well: #it connects commands to buttons for i in range(10): getattr(ui, 'btn%s' % i).clicked.connect(lambda i, j=i: …