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

It is possible to customize the response to nested object attributes?

I'm trying to figure out if there's a way to return one attribute of a nested object when the attribute is addressed using the 'dot' notation, but to return different attributes of that object when subsequent attributes are requested. ex) class…
FoitGuy
  • 35
  • 7
0
votes
1 answer

Get content of a string variable using getattr

I would like to know how to retrieve the content of a string variable so that I can use its value as an argument name in a function. Here the code: import pandas import jinja2 oDateList = ['2017-03-22','2017-03-23','2017-03-24'] oData =…
Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77
0
votes
1 answer

Accessing undefined method

I wanted to do some handling of undefined methods or attributes calls in python class. Where if method is not present it is required to perform some task. Here in the example I have written some methods and some calls. What I want to achieve is, if…
IT.dumb
  • 71
  • 6
0
votes
1 answer

Why is __getattr__ not called for indexing operations?

My question: It seems that __getattr__ is not called for indexing operations, ie I can't use __getattr__ on a class A to provide A[...]. Is there a reason for this? Or a way to get around it so that __getattr__ can provide that functionality…
Sam Bader
  • 185
  • 1
  • 7
0
votes
2 answers

Calling a function with a variable

I am looping through a directory to send specific files of interest (contained in a list) to their corresponding functions, which are named similarly to the items in the list. import os,sys,argparse import Contact_parse import Notes_parse import…
the_word
  • 15
  • 1
  • 3
0
votes
1 answer

Python: no attribue __hash__

Instances of PackOfPersons have a __getattr__() function that, for example: PackOfPersons.say_name() will call Person.say_name() for Person instances inside PackOfPersons.person_objects(). class Person: def __init__(self, name): …
Jonathan
  • 303
  • 4
  • 14
0
votes
1 answer

Parsing Object Attribute with an Input Value

I'm starting to think this is something that may not be doable in Python in an easy way. Either that or I cannot seem to word this question well enough for Google to solve it (or to find a StackOverflow answer of it). Here is some lite code to help…
abby sobh
  • 1,574
  • 19
  • 15
0
votes
1 answer

Wrapping stdin/stdout causes IPython to lose auto completion and history features

I'm working on a script that uses the embedded shelling capabilities of IPython and as a requirement, it has to log all the data from stdin/stdout into a file. For this reason I decided to write wrappers for them; however, after switching out the…
Miro Markaravanes
  • 3,285
  • 25
  • 32
0
votes
1 answer

Get all functions inside a Python Package for getattr

I have two python files and both at same directory level like this project |> tasks.py |> queue.py I have a function like this in tasks.py file def foo(message): # perform some functions on message data # blah blah and another…
Muhammad Hassan
  • 14,086
  • 7
  • 32
  • 54
0
votes
1 answer

__getattr__ error while calling foreach for dataframe in pyspark

I am trying to parse a SQL query, and want to call a function for each row of a dataframe. Function is as below: def updateParser(df): # update tab1 set value1 = 0.34 where id = 1111 # identify positions setPos = df.select(instr(df.query, ' set…
manmeet
  • 330
  • 2
  • 4
  • 15
0
votes
2 answers

Python, using two variables in getattr?

I'm trying to do the following: import sys; sys.path.append('/var/www/python/includes') import functionname x = 'testarg' fn = "functionname" func = getattr(fn, fn) func (x) but am getting an error: "TypeError: getattr(): attribute name must be…
Rick
  • 16,612
  • 34
  • 110
  • 163
0
votes
1 answer

getattr for a list index in Python?

I know I can do a try/except or if/else and set a default based on the error or else clause, but I was just wondering if there was a one liner that could do it like getattr can.
13steinj
  • 427
  • 2
  • 9
  • 16
0
votes
1 answer

__getattribute__ function to return object value from another class

I have a base class which has 2 attributes which itself are from another class. See the code below class Base(object): def __init__(self, obj1, obj2): self.obj1 = obj1 self.obj2 = obj2 def __getattribute__(self, name): …
Anurag Sharma
  • 4,839
  • 13
  • 59
  • 101
0
votes
1 answer

In Python 3, calling a Class function by name before init with inheritance

The goal: B in inherits from A. A and B have a factory method create, which harmonizes different input types before initializing the actual class. create calls different create methods create_general_1, create_general_2, create_specific_b_1 via…
elke
  • 1,220
  • 2
  • 12
  • 24
0
votes
1 answer

Lazy Evaluation: forward operations to deferred value

I have implemented classes for lazily evaluating configurations that are dumped to JSON. No problem there, just extend the encoder to actively evaluate the classes using a specific protocol (fixed method/property). class DeferredCall(object): …
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119