Programmatically setting an attribute in Python?
Posted
by Nick
on Stack Overflow
See other posts from Stack Overflow
or by Nick
Published on 2008-11-12T19:33:07Z
Indexed on
2010/03/21
10:51 UTC
Read the original article
Hit count: 307
Suppose I have a python object x
and a string s
, how do I set the attribute s
on x
? So:
>>> x = SomeObject()
>>> attr = 'myAttr'
>>> # magic goes here
>>> x.myAttr
'magic'
What's the magic? The goal of this, incidentally, is to cache calls to x.__getattr__()
.
© Stack Overflow or respective owner