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

Accessing a method using getattr

When creating a new class instance, I'm trying to call a method in a different class however can't get it to work. Here's what I have: class DataProject(object): def __init__(self,…
pandita
  • 4,739
  • 6
  • 29
  • 51
0
votes
1 answer

Jython 2.1 __getattr__

I am trying to implement a wrapper/proxy class for a java object (baseClient) in jython v2.1. Everything seems to be working ok except when the following statement is encountered: if __client != None # __client is an instance of the ClientProxy…
user213089
0
votes
1 answer

dynamically asigning instances in Python/PyQt4

Ok, this might be a duplicate, but as I couldn't really get anything out of (possibly) similar questions, here is mine: I'm working on a small PyQt4 program where I can enter the name of a song in a QLineEdit and then add a QLabel of it beneath it.…
Peter Goldsborough
  • 1,366
  • 16
  • 20
0
votes
1 answer

strange behavior with lamba: getattr(obj, x) inside a list

In the following example: class A(object): pass prop1 = 1 prop2 = 2 prop3 = 3 prop4 = 4 obj = A() tmp = ['prop1', 'prop2', 'prop3', 'prop4'] getter = [ lambda: getattr(obj, x) for x in tmp ] I am always getting 4 when calling…
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
0
votes
1 answer

What's the best way to get referenced object (variable level)

Let's assume, the following case: class foo(object): def __init__(self, ref=None): self.ref = ref def __str__(self): return "I'm foolish" f = foo() f.ref = foo() f.ref.ref = foo() f.ref.ref.ref = foo() Trying …
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56
0
votes
1 answer

Wrapping and synchronizing messenger class in python

I want to create a structure with a messenger class which is shared between several other classes. I want to keep parameters synchronized between the messenger and the sub-classes, and I also want the sub-classes to be able to access each other via…
AJ Medford
  • 323
  • 1
  • 2
  • 10
0
votes
1 answer

Why does Python look for __members__ rather than the requested field?

I have a class that defines its own __getattr__() in order to interact with an XML tree instantiated objects contain. This hides the XML structure from the user and allows him to set tag values, etc. as if they were normal fields on the object and…
Daniel Lee
  • 2,030
  • 1
  • 23
  • 29
0
votes
2 answers

Convert string to function in python

I have a string coming from the database. This string is the name of a file .py that is within a module. The structure is this: files ├── file1.py ├── file2.py └── __init__.py The file1.py contain: def file1(imprime): print(imprime) The…
0
votes
2 answers

How to turn these functions generic

I wanted to shorten my code, since i`m having more functions like this. I was wondering if I could use getattr() to do something like this guy asked. Well, here it goes what I`ve got: def getAllMarkersFrom(db, asJSON=False): '''Gets all markers…
cesarvargas
  • 182
  • 3
  • 15
0
votes
2 answers

__getattr__ within a module in python

I am working with python. I want to know whether or not any method is existed or not in same module. I think getattr() does this but I couldn't do. Here is sample code saying what really I want to do with. #python module is my_module.py def…
ln2khanal
  • 949
  • 1
  • 8
  • 16
0
votes
2 answers

Smartly handle different permissions with inheritance

I have to manage permissions and I have different user types, as for example a couple here. def not_allowed(*args, **kwargs): return False class User(object): def __init__(self, userid): self.userid = userid def __getattr__(self,…
andrea_crotti
  • 3,004
  • 2
  • 28
  • 33
0
votes
2 answers

Class confusion in Python with the getattr

A similar question was asked here . However, the responses didn't really help me grasp how some parts of the program worked. The program is as follows: from sys import exit from random import randint class Game(object): def __init__(self,…
kiddsupreme
  • 115
  • 1
  • 3
  • 13
-1
votes
1 answer

Calling method with arguments using __getattr__()

Recently, I saw the code below as an answer to a problem: class Proxy: def __init__(self, obj): self._obj = obj def __getattr__(self, attr): try: value = getattr(self._obj, attr) except Exception: …
Armaho
  • 37
  • 5
-1
votes
2 answers

How to Call All Function from one File and then Move to other File Again Call the Functions using getattr function in Python

suppose I have two files File1.py and then File2.py and the Multiple Functions Written in the Files, and then I have the main.py file where I am calling the File1.py functions using getattr builtin function. after reading one by one function of the…
-1
votes
1 answer

How the getattr() method works on this HackerRank case?

I recently began to learn programming. During a HackerRank problem solving session, this one came up: You are given a set A and N number of other sets. These N number of sets have to perform some specific mutation operations on set A. Your task is…
dgr
  • 1