Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically compose and access class attributes in Python? [duplicate]

Tags:

python

I have a Python class that have attributes named: date1, date2, date3, etc.

During runtime, I have a variable i, which is an integer.

What I want to do is to access the appropriate date attribute in run time based on the value of i.

For example,

if i == 1, I want to access myobject.date1

if i == 2, I want to access myobject.date2

And I want to do something similar for class instead of attribute.

For example, I have a bunch of classes: MyClass1, MyClass2, MyClass3, etc. And I have a variable k.

if k == 1, I want to instantiate a new instance of MyClass1

if k == 2, I want to instantiate a new instance of MyClass2

How can i do that?

EDIT

I'm hoping to avoid using a giant if-then-else statement to select the appropriate attribute/class.

Is there a way in Python to compose the class name on the fly using the value of a variable?

like image 819
Continuation Avatar asked Jul 20 '10 00:07

Continuation


People also ask

What does __ add __ do in Python?

__add__ magic method is used to add the attributes of the class instance. For example, let's say object1 is an instance of a class A and object2 is an instance of class B and both of these classes have an attribute called 'a', that holds an integer.

How do you dynamically create a class object in Python?

Python Code can be dynamically imported and classes can be dynamically created at run-time. Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.

How do you access the class property in Python?

Accessing the attributes of a classgetattr() − A python method used to access the attribute of a class. hasattr() − A python method used to verify the presence of an attribute in a class. setattr() − A python method used to set an additional attribute in a class.

What is __ Getattribute __ in Python?

__getattribute__This method should return the (computed) attribute value or raise an AttributeError exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, object.


2 Answers

You can use getattr() to access a property when you don't know its name until runtime:

obj = myobject() i = 7 date7 = getattr(obj, 'date%d' % i) # same as obj.date7 

If you keep your numbered classes in a module called foo, you can use getattr() again to access them by number.

foo.py:   class Class1: pass   class Class2: pass   [ etc ]   bar.py:   import foo   i = 3   someClass = getattr(foo, "Class%d" % i) # Same as someClass = foo.Class3   obj = someClass() # someClass is a pointer to foo.Class3   # short version:   obj = getattr(foo, "Class%d" % i)() 

Having said all that, you really should avoid this sort of thing because you will never be able to find out where these numbered properties and classes are being used except by reading through your entire codebase. You are better off putting everything in a dictionary.

like image 198
too much php Avatar answered Oct 03 '22 06:10

too much php


For the first case, you should be able to do:

getattr(myobject, 'date%s' % i) 

For the second case, you can do:

myobject = locals()['MyClass%s' % k]() 

However, the fact that you need to do this in the first place can be a sign that you're approaching the problem in a very non-Pythonic way.

like image 23
Aram Dulyan Avatar answered Oct 03 '22 08:10

Aram Dulyan