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
1
vote
2 answers

python __getattr__ get multiple value get

python class module __getattr__ use funation obj.val1.val2 error. python program for. class hub: def __getattr__(self,__name): print(__name) obj = hub() obj.val1.val2 error: val1 Traceback (most recent call last): File "hub.py", line…
THAVASI.T
  • 131
  • 1
  • 5
1
vote
1 answer

Using __getattr__() and __setattr__() to provide multiple ways to access child variables

Esteemed colleagues! Please consider... #!/usr/bin/env python3.6 class Child(object): def __init__(self, name:str, value = 0): self.name = name; self.value = value; def __repr__(self): return ' child "{}" has value…
Lance E.T. Compte
  • 932
  • 1
  • 11
  • 33
1
vote
0 answers

Why does getattr catch all AttributeErrors and not just the attribute it's looking for?

The following is an issue I've observed in Python 3.8. Let's say I define a simple class: class A: @property def b(self): return self.c # should throw AttributeError Accessing A().b fails as expected (see below), but here's my…
snarayanan
  • 21
  • 1
1
vote
2 answers

__getattr__ for class atributes?

For a class Foo, class Foo: ... is there a way to call a specific method whenever Foo.XXX (an arbitrary class attribute XXX, such as bar, bar2, etc.) is being resolved, but NOT whenever Foo().XXX (an arbitrary instance attribute) is resolved?…
Captain Trojan
  • 2,800
  • 1
  • 11
  • 28
1
vote
2 answers

Python call function with getattr and args/kwargs from a string

I want to call a module function with getattr (or maybe something else?) from a string like this: import bar funcStr = "myFunc(\"strParam\", 123, bar.myenum.val1, kwarg1=\"someString\", kwarg2=456, kwarg3=bar.myenum.val2)" [function, args, kwargs] =…
stackomatiker
  • 324
  • 3
  • 14
1
vote
1 answer

__getattr__ for module running twice on import?

I'm trying to run some logic when objects are imported from a module by using __getattr__ as described here: __getattr__ on a module ...but the logic runs twice. Why is that happening and how can I stop it? main.py from test import x,…
pyjamas
  • 4,608
  • 5
  • 38
  • 70
1
vote
0 answers

Jupyter notebook / Ipython autocomplete with __getattr__

I have a class for which I defined __getattr__ to pull attribute data from a YAML file dynamically. It looks something like this: class Example: def __init__(self, yaml_path): self.rand = 0 with open(yaml_path) as file: …
1
vote
1 answer

Use getattr on function

How can I use getattr on a function? This is how I use it normally: df = candlestick.inverted_hammer(df, target='result') But how can I do this on getattr? I did as follow but it does not work: candle_names = [ 'inverted_hammer', 'hammer', …
amirt4
  • 136
  • 7
1
vote
1 answer

Checking if getattr exists within a dictionary comprehension in Python3

I have a line of code attributes_dict["data_properties"] = { prop: getattr(ontology_node, prop) for prop in data_properties } However, not every ontology_node will have all of the properties, so I'd like to do something similar to the…
1
vote
0 answers

Add documentation of attributes generated on runtime using Sphinx

My class implements a __getattr__ method that, for a limited number of accepted keys, returns valid data. I would like to add the docstring documentation inside the class for these few keys so that they are listed exactly as the standard properties…
Ghard
  • 11
  • 2
1
vote
2 answers

Access object methods from getattr()

I need to create a script that allows you to access object atributes dynamically from getattr() built-in function, but I'm having lots of troubles when trying to access the object methods, here is an example: class Dog(): def __init__(self): …
Stack
  • 1,028
  • 2
  • 10
  • 31
1
vote
4 answers

What does this (simple?) expression in Python mean? func(self)(*args)

I saw some Python code like: getattr(self, that)(*args). what does it mean? I see that the builtin getattr function gets called, passing the current object and that; but what is the (*args) doing after that? Does it call something with *args as…
rapadura
  • 5,242
  • 7
  • 39
  • 57
1
vote
0 answers

Pass parameters into xbbg's bhd function dynamically

I need to make a bloomberg request using xbbgs' bdh function. I want to pass on additional parameters, which is not working properly. My request: from xbbg import blp blp.bdh("LBUSTRUU Index","OPTION_ADJ_DURATION_SOV_CRV","2020-07-08…
Jogi
  • 304
  • 4
  • 15
1
vote
1 answer

debugging pycharm __gettattr__

I have a problem with debugging in PyCharm (maybe this problem is observable in other IDEs too): If you take the following code: class Test1: def __init__(self): self.arg1 = 'abc' self.arg2 = 'def' self.some_other_class =…
Egirus Ornila
  • 1,234
  • 4
  • 14
  • 39
1
vote
1 answer

Is there a library to follow a sequence of indexing and `getattr` operations?

I have a bunch of objects, they have members, their members have members, ..., somewhere I need to do indexing, and then access members ... So, basically, I want to get obj.member1.member2[3].member4 and I also want to assign…
CrabMan
  • 1,578
  • 18
  • 37