Attribute error-- but I'm innocent(?)

Discussion in 'Python' started by Nick Mellor, Mar 3, 2009.

  1. Nick Mellor

    Nick Mellor Guest

    Hi all,

    I'm pretty sure I'm following all the Python rules: I've put "self"
    before "forename" to make sure it's treated as a data attribute
    (instance variable.) And from within a class, I'm told, you need to
    prefix the var with self too. RandomName is a class that I've tested
    (and which still works.) So why do I get this error?

    File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
    return {"Forename" : self.forename.randomByWeight(),
    AttributeError: RandomPerson instance has no attribute 'forename'

    Here's the code (Python 2.6, PythonWin):

    class RandomPerson:
    def __init(self):
    self.forename = RandomName("h:\\Testing\\NameDb\
    \Forenames.csv", namefield = "Forename")
    self.surname = RandomName("h:\\Testing\\NameDb\\Surnames.csv",
    namefield = "Surname")
    self.randomAddress = dictfile("h:\\Testing\\NameDb\
    \Addresses.csv").cycleShuffled()
    [...]

    def randomName(self):
    return {"Forename" : self.forename.randomByWeight(),
    "Surname" : self.surname.randomByWeight()}

    if __name__ == "__main__":
    person = RandomPerson()
    print person.randomName()
    Nick Mellor, Mar 3, 2009
    #1
    1. Advertising

  2. Nick Mellor

    Chris Rebert Guest

    On Mon, Mar 2, 2009 at 4:56 PM, Nick Mellor
    <> wrote:
    > Hi all,
    >
    > I'm pretty sure I'm following all the Python rules: I've put "self"
    > before "forename" to make sure it's treated as a data attribute
    > (instance variable.) And from within a class, I'm told, you need to
    > prefix the var with self too. RandomName is a class that I've tested
    > (and which still works.) So why do I get this error?
    >
    >  File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
    >    return {"Forename" : self.forename.randomByWeight(),
    > AttributeError: RandomPerson instance has no attribute 'forename'
    >
    > Here's the code (Python 2.6, PythonWin):
    >
    > class RandomPerson:
    >    def __init(self):


    That previous line is supposed to be:
    def __init__(self):

    Note the trailing underscores.

    Cheers,
    Chris

    --
    Shameless self-promotion:
    http://blog.rebertia.com
    Chris Rebert, Mar 3, 2009
    #2
    1. Advertising

  3. Nick Mellor

    John Machin Guest

    On Mar 3, 11:56 am, Nick Mellor <> wrote:
    > Hi all,
    >
    > I'm pretty sure I'm following all the Python rules: I've put "self"
    > before "forename" to make sure it's treated as a data attribute
    > (instance variable.) And from within a class, I'm told, you need to
    > prefix the var with self too. RandomName is a class that I've tested
    > (and which still works.)


    Doesn't look like you've executed RandomName() in this particular
    example.


    > So why do I get this error?
    >
    >   File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
    >     return {"Forename" : self.forename.randomByWeight(),
    > AttributeError: RandomPerson instance has no attribute 'forename'


    Next time, show the *full* traceback.

    >
    > Here's the code (Python 2.6, PythonWin):
    >
    > class RandomPerson:
    >     def __init(self):


    Because you named this method __init instead of __init__

    Next time, before proclaiming innocence, insert a print statement or
    two:
    print "Hello from RandomPerson.__init"
    and wonder what the problem is if the print statement is not executed
    >         self.forename = RandomName("h:\\Testing\\NameDb\
    > \Forenames.csv", namefield = "Forename")
    >         self.surname = RandomName("h:\\Testing\\NameDb\\Surnames.csv",
    > namefield = "Surname")
    >         self.randomAddress = dictfile("h:\\Testing\\NameDb\
    > \Addresses.csv").cycleShuffled()
    > [...]
    >
    >     def randomName(self):
    >         return {"Forename" : self.forename.randomByWeight(),
    >                 "Surname" : self.surname.randomByWeight()}
    >
    > if __name__ == "__main__":
    >     person = RandomPerson()


    and here's another good place for a print statement:
    print person.__dict__

    >     print person.randomName()


    Cheers,
    John
    John Machin, Mar 3, 2009
    #3
  4. Nick Mellor

    Nick Mellor Guest

    Thanks Chris and John, all workin now. Sorry about proclamation of
    innocence-- fruitless morning and 3 hours sleep :)

    Nick

    On Mar 3, 12:03 pm, Chris Rebert <> wrote:
    > On Mon, Mar 2, 2009 at 4:56 PM, Nick Mellor
    >
    >
    >
    > <> wrote:
    > > Hi all,

    >
    > > I'm pretty sure I'm following all the Python rules: I've put "self"
    > > before "forename" to make sure it's treated as a data attribute
    > > (instance variable.) And from within a class, I'm told, you need to
    > > prefix the var with self too. RandomName is a class that I've tested
    > > (and which still works.) So why do I get this error?

    >
    > >  File "h:\Testing\NameDb\dictfile.py", line 107, in randomName
    > >    return {"Forename" : self.forename.randomByWeight(),
    > > AttributeError: RandomPerson instance has no attribute 'forename'

    >
    > > Here's the code (Python 2.6, PythonWin):

    >
    > > class RandomPerson:
    > >    def __init(self):

    >
    > That previous line is supposed to be:
    >    def __init__(self):
    >
    > Note the trailing underscores.
    >
    > Cheers,
    > Chris
    >
    > --
    > Shameless self-promotion:http://blog.rebertia.com
    Nick Mellor, Mar 3, 2009
    #4
  5. Nick Mellor

    Denis Kasak Guest

    On Tue, Mar 3, 2009 at 6:13 AM, Dennis Lee Bieber <> wrote:
    > On Mon, 2 Mar 2009 16:56:58 -0800 (PST), Nick Mellor
    > <> declaimed the following in
    >>
    >>     def __init(self):
    >>         self.forename = RandomName("h:\\Testing\\NameDb\
    >> \Forenames.csv", namefield = "Forename")

    >
    >        Where is "RandomName" defined? Python is case sensitive, and the
    > method further down is "randomName".


    You could ask the same for dictfile(), which is also not shown, though
    you could probably deduce from the filename (and the fact that it
    works now) that it is defined elsewhere in the same file.

    >        Furthermore, given the indentation you show, randomName is a method
    > of RandomPerson... If that is true, you'll need to do
    >
    >                target = self.randomName(...)
    >
    >>         self.surname = RandomName("h:\\Testing\\NameDb\\Surnames.csv",
    >> namefield = "Surname")
    >>         self.randomAddress = dictfile("h:\\Testing\\NameDb\
    >> \Addresses.csv").cycleShuffled() it should be obvious
    >> [...]
    >>
    >>     def randomName(self):


    As the OP said, RandomName is a class and, judging by the
    capitalization, he is instantiating objects of the said class and not
    the method of RandomPerson.

    >        Up above you are pass two arguments (besides the "self" with my
    > noted change would supply), but here you do not have anything to accept
    > them... Where is that file name and that keyword argument supposed to be
    > received?
    >
    >        def randomName(self, fileid, namefield):
    >        #have to use "namefield" as you used a keyword passing mechanism
    >
    >>         return {"Forename" : self.forename.randomByWeight(),
    >>                 "Surname" : self..surname.randomByWeight()}
    >>

    >        This will return a dictionary containing both forename and
    > surname... Assuming you have forename and surname OBJECTS with contain a
    > randomByWeight method.


    In all probability, forename and surname *are* instances of RandomName
    and contain the randomByWeight() method.

    --
    Denis Kasak
    Denis Kasak, Mar 3, 2009
    #5
    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. Bostonasian
    Replies:
    1
    Views:
    817
    Joris Gillis
    Sep 18, 2005
  2. Replies:
    2
    Views:
    1,076
    Henry S. Thompson
    Mar 6, 2006
  3. valued customer

    Completely INNOCENT Indentation question

    valued customer, Oct 13, 2004, in forum: Python
    Replies:
    8
    Views:
    306
    Caleb Hattingh
    Oct 15, 2004
  4. johnny
    Replies:
    5
    Views:
    721
    johnny
    Dec 10, 2006
  5. ffxff
    Replies:
    0
    Views:
    297
    ffxff
    Jul 12, 2008
Loading...

Share This Page