[Tutor] variable name based on variables (expansion?)

Martin Walsh mwalsh at groktech.org
Mon Feb 28 15:44:47 CET 2005


John Christian wrote:

># But I want the assignment
># to be based on variables
>LIST=1
>POSITION=2
>
>GameLogic.varList$LIST[$POSITION]=0
>
>  
>
 >>> help(getattr)

Help on built-in function getattr:

getattr(...)
    getattr(object, name[, default]) -> value

    Get a named attribute from an object; getattr(x, 'y') is equivalent 
to x.y.
    When a default argument is given, it is returned when the attribute 
doesn't
    exist; without it, an exception is raised in that case.

from http://docs.python.org/lib/built-in-funcs.html

*getattr*( 	object, name[, default])

    Return the value of the named attributed 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. 

#---------------------------

LIST = 1
POSITION = 2

getattr(GameLogic, 'varList'+str(LIST))[POSITION] = 0


>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around 
>http://mail.yahoo.com 
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>



More information about the Tutor mailing list