using variable-value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tor Erik Sønvisen

    #1

    using variable-value

    Hi

    In php I can assign a value to a variable and use this varaible to access a
    property in some object:

    $var = 'property';
    $object->{$var}

    This will transelate to $object->property...
    Is this possible in Python?

    # Prints help on methods in Canvas-instance
    for method in dir(self.canvas ):
    print method
    print help(self.canva s.method)

    gives me " AttributeError: Canvas instance has no attribute 'method' "...

    regards tores


  • bruno modulix

    #2
    Re: using variable-value

    Tor Erik Sønvisen wrote:[color=blue]
    > Hi
    >
    > In php I can assign a value to a variable and use this varaible to access a
    > property in some object:
    >
    > $var = 'property';
    > $object->{$var}
    >
    > This will transelate to $object->property...
    > Is this possible in Python?[/color]

    Not directly, but there's a way: getattr(obj, attname, [,default])
    [color=blue]
    > # Prints help on methods in Canvas-instance
    > for method in dir(self.canvas ):
    > print method
    > print help(self.canva s.method)
    >
    > gives me " AttributeError: Canvas instance has no attribute 'method' "...[/color]

    Try this
    for method in dir(self.canvas ):
    print method
    print help(getattr(se lf.canvas, "method"))

    HTH
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    Working...