convert string literal to object attribute

Discussion in 'Python' started by BiraRai, Oct 31, 2008.

  1. BiraRai

    BiraRai Guest

    def getAttributeForProperty(self,rollnumber,attribute):
    # attribute have the value _ward
    '''
    If year is null then use current year.
    Returns the value of the attribute for the given roll number
    '''
    print 'Searching for attribute', attribute
    for index, i in enumerate(self._aa):
    if (rollnumber == i._roll_number) and ( hasattr(i,attribute) ) :
    print index,i.attribute (COMPILER ERROR HERE)
    return i.attribute
    return
    print "\n"
    return

    i want i.attribute to be treated as i._ward

    I get a compile error "instance has no attribute 'attribute' " which i
    understand. how do i fix this

    HELP
    BiraRai, Oct 31, 2008
    #1
    1. Advertising

  2. On Fri, 31 Oct 2008 11:40:02 -0700, BiraRai wrote:

    > def getAttributeForProperty(self,rollnumber,attribute):
    > # attribute have the value _ward
    > '''
    > If year is null then use current year. Returns the value of the
    > attribute for the given roll number '''
    > print 'Searching for attribute', attribute for index, i in
    > enumerate(self._aa):
    > if (rollnumber == i._roll_number) and ( hasattr

    (i,attribute) ) :
    > print index,i.attribute (COMPILER ERROR HERE) return

    i.attribute
    > return
    > print "\n"
    > return
    >
    > i want i.attribute to be treated as i._ward


    Look at the `getattr()` function.

    > I get a compile error "instance has no attribute 'attribute' " which i
    > understand. how do i fix this


    It's not a compile error but a runtime error.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Oct 31, 2008
    #2
    1. Advertising

  3. BiraRai

    BiraRai Guest

    On Oct 31, 3:00 pm, Marc 'BlackJack' Rintsch <> wrote:
    > On Fri, 31 Oct 2008 11:40:02 -0700, BiraRai wrote:
    > > def getAttributeForProperty(self,rollnumber,attribute):
    > >    # attribute have the value _ward
    > >    '''
    > >    If year is null then use current year. Returns the value of the
    > >    attribute for the given roll number '''
    > >    print 'Searching for attribute', attribute for index, i in
    > >    enumerate(self._aa):
    > >        if (rollnumber == i._roll_number) and ( hasattr

    > (i,attribute) ) :
    > >            print index,i.attribute (COMPILER ERROR HERE) return

    > i.attribute
    > >            return
    > >    print "\n"
    > >    return

    >
    > > i want i.attribute to be treated as i._ward

    >
    > Look at the `getattr()` function.
    >
    > > I get a compile error "instance has no attribute 'attribute' " which i
    > > understand.  how do i fix this

    >
    > It's not a compile error but a runtime error.
    >
    > Ciao,
    >         Marc 'BlackJack' Rintsch


    Thanks Marc, worked great.
    BiraRai, Oct 31, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prakash Prabhu

    String literal and String Object

    Prakash Prabhu, Aug 27, 2003, in forum: Java
    Replies:
    3
    Views:
    621
    John C. Bollinger
    Aug 27, 2003
  2. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    355
    Anonieko Ramos
    Sep 27, 2004
  3. Marc Aymerich
    Replies:
    0
    Views:
    61
    Marc Aymerich
    Nov 24, 2013
  4. Devin Jeanpierre
    Replies:
    12
    Views:
    92
    Steven D'Aprano
    Nov 24, 2013
  5. Chris Angelico
    Replies:
    0
    Views:
    54
    Chris Angelico
    Nov 24, 2013
Loading...

Share This Page