How to Access Object Attribute Given String Corresponding to Name of That Attribute

How to access object attribute given string corresponding to name of that attribute

There are built-in functions called getattr and setattr

getattr(object, attrname)
setattr(object, attrname, value)

In this case

x = getattr(t, 'attr1')
setattr(t, 'attr1', 21)

Python: access class property from string

x = getattr(self, source) will work just perfectly if source names ANY attribute of self, including the other_data in your example.

Python string to attribute

Use the builtin function getattr.

getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

Get an object attribute

To access field or method of an object use dot .:

user = User()
print user.fullName

If a name of the field will be defined at run time, use buildin getattr function:

field_name = "fullName"
print getattr(user, field_name) # prints content of user.fullName

Python: Reference an object attribute by variable name?

You can use the getattr function:

property_name = getattr(Game, dictname)

Accessing an attribute using a variable in Python

The expression this_prize.choice is telling the interpreter that you want to access an attribute of this_prize with the name "choice". But this attribute does not exist in this_prize.

What you actually want is to return the attribute of this_prize identified by the value of choice. So you just need to change your last line using the getattr() method...

from collections import namedtuple

import random

Prize = namedtuple("Prize", ["left", "right" ])

this_prize = Prize("FirstPrize", "SecondPrize")

if random.random() > .5:
choice = "left"
else:
choice = "right"

# retrieve the value of "left" or "right" depending on the choice

print "You won", getattr(this_prize, choice)

How to access object attribute given string corresponding to name of that attribute

There are built-in functions called getattr and setattr

getattr(object, attrname)
setattr(object, attrname, value)

In this case

x = getattr(t, 'attr1')
setattr(t, 'attr1', 21)

Is there a way to access just the value of object attribute when calling it as a variable

I'm assuming you're throwing all of your constants in a file called constants.py somewhere. If so, the quick-and-dirty solution is to use __dict__.

import constants

my_favorite_color = constants.__dict__[color_name]

but that's messy and would confuse folks reading the code. If you intend that constants be accessed as a dictionary, then it would be best to make that explicit. In constants.py, consider

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# ... more colors

# Then at the bottom
COLORS = {
'BLACK': BLACK,
'WHITE': WHITE,
}

Then you can use it as

from constants import COLORS

my_favorite_color = COLORS[color_name]

It's a bit more verbose, but in the words of a wise man, "explicit is better than implicit".



Related Topics



Leave a reply



Submit