Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: python "no attribute "

  1. #1
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    19

    python "no attribute "

    When I try to run this, it gave me this error. What did I do wrong?

    Thanks

    File "D:\Python26\my_module.py", line 25, in <module>
    main()
    File "D:\Python26\my_module.py", line 21, in main
    for i in range(len(X.readColumn.names)):
    AttributeError: 'function' object has no attribute 'names'
    Code:
    import csv
    
    class Reader(object):
        def __init__(self):
            print "Welcome"
        def readColumn(self,fileReader,fileReader_list=[],names=[],idigit=[]):
            self.fileReader = csv.reader(open("survey_result.csv", "rb"))
            self.fileReader_list = fileReader_list
            self.fileReader_list.extend(self.fileReader)
            self.names = names
            self.idigit = idigit
            for column in self.fileReader_list:
                self.names.append(column[1])
                self.idigit.append(column[2])
            del self.names[0]
            del self.idigit[0]
            self.names.sort()
    
    def main():
        X = Reader()
        for i in range(len(X.readColumn.names)):
                print X.readColumn.names[i], X.readColumn.idigit[i]
    
    if __name__ == '__main__':
        main()
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: python "no attribute "

    Code:
    for i in range(len(X.readColumn.names)):
        print X.readColumn.names[i], X.readColumn.idigit[i]
    You don't need to use "X.readColumn", when you declared names in self (self.names = names), that became a member of the object itself, not a local variable to the readColumn function. Do this:
    Code:
    for i, name in enumerate(X.names):
        print name, x.idigit[i]
    That should work.
    Wow I changed my sig!

  4. #3
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    19

    Re: python "no attribute "

    Hi, Zerk. Thanks for the reply.

    I tried it but still no luck.

    Code:
    import csv
    
    class Reader(object):
        def __init__(self):
            pass
        def readColumn(self,fileReader,fileReader_list=[],names=[],idigit=[]):
            self.fileReader = csv.reader(open("survey_result.csv", "rb"))
            self.fileReader_list = fileReader_list
            self.fileReader_list.extend(self.fileReader)
            self.names = names
            self.idigit = idigit
            for column in self.fileReader_list:
                self.names.append(column[1])
                self.idigit.append(column[2])
            del self.names[0]
            del self.idigit[0]
            self.names.sort()
    
    X = Reader()
    for i in range(len(X.names)):
        print X.names[i], X.idigit[i]
    Traceback (most recent call last):
    File "D:\Python26\my_module.py", line 20, in <module>
    for i in range(len(X.names)):
    AttributeError: 'Reader' object has no attribute 'names'
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  5. #4
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: python "no attribute "

    This is because you haven't ran the readColumn function yet. You'll need to run it or those values will not be added to the X reader object. You could add these in the constructor.

    EDIT: Wait a tic...
    Code:
            del self.names[0]
            del self.idigit[0]
    What's this for?
    Wow I changed my sig!

  6. #5
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    19

    Re: python "no attribute "

    Hi Zerk,

    This script uses csv to read a csv data file. Each column is read as a list. That you know already. First row is the title of each individual column, so i delete them for my purpose.

    Code:
    X = Reader()
    for i, names in enumerate(X.names):
        print names, X.idigit[i]
    I kinda got what you meant but I am still unable to run the script with your implementation


    even with this
    Code:
            for column in self.fileReader_list:
                names.append(column[1])
                idigit.append(column[2])
            del names[0]
            del idigit[0]
            names.sort()
            
    X = Reader()
    for i, names in enumerate(X.names):
        print names, X.idigit[i]
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  7. #6
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: python "no attribute "

    Interesting name... "Zerk"...

    Anyway, like I said, the readColumn object must be ran before those data members will be part of the X object. Python is not a statically typed language, further, you can simply add new members to objects even if those members don't ordinarily exist:
    Code:
    X = MyObjectWithoutMemberPie()
    X.Pie = 5
    So if you want names and idigits to ALWAYS be members of the Reader object, you'll need to add them in the constructor:
    Code:
        def __init__(self):
            self.names = []
            self.idigit = []
    Wow I changed my sig!

  8. #7
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    19

    Re: python "no attribute "

    Hi Zeke
    Sorry I am very careless.

    Hmmm I think I got you in some ways, and this was fine
    Code:
    import csv
    
    class Reader(object):
        def __init__(self):
            self.names = []
            self.idigit = []
            self.fileReader_list=[]
            self.fileReader = csv.reader(open("survey_result.csv", "rb"))
            self.fileReader_list.extend(self.fileReader)
            for column in self.fileReader_list:
                self.names.append(column[1])
                self.idigit.append(column[2])
            del self.names[0]
            del self.idigit[0]
    
    X = Reader()
    for i, names in enumerate(X.names):
        print names, X.idigit[i]
    Thanks. I will continue to investigate writing this.
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  9. #8
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    19

    Re: python "no attribute "

    Hi, I just came across with a weird (well silly to expert :[ ) problem
    Code:
    import csv
    
    # let us first make student an object
    
    class Student(object):
        def __init__(self):
            #make sure we can access them
            self.fname = []
            self.lname = []
            self.gender = []
            self.ID = []
            #make sure we read it right away
            self.datalst = []
            self.Reader = csv.reader(open("copy-john.csv", "rb"))
            self.datalst.extend(self.Reader)
            def Datahandler(self):
                for column in self.datalst:
                    self.fname.append(column[0])
                    self.lname.append(column[1])
                    self.ID.append(column[2])
                del self.fname[0],self.lname[0],self.ID[0]
        def Tableformat(self):
            print "%-14s|%-11s|%4s" %('First Name','Last Name','ID')
            print "-" * 39
            for (i, fname) in enumerate(self.fname):
               print "%-14s|%-10s|%5s" %(fname,self.lname[i],self.ID[i])
                
    X = Student()
    X.Tableformat()
    This was my first attempt, and output as below:
    >>> ================================ RESTART ================================
    >>>
    First Name |Last Name | ID
    ---------------------------------------
    You see, no data retrieved.

    But if I removed this definition
    def Datahandler(self):
    and let codes below to move into __init__, then I could pull out the data. The code would work out as it was.

    Why can't I separate them? If I have to, what should I do?
    Thanks

    Code below worked fine.
    Code:
    import csv
    
    # let us first make student an object
    
    class Student(object):
        def __init__(self):
            #make sure we can access them
            self.fname = []
            self.lname = []
            self.gender = []
            self.ID = []
            #make sure we read it right away
            self.datalst = []
            self.Reader = csv.reader(open("copy-john.csv", "rb"))
            self.datalst.extend(self.Reader)
            for column in self.datalst:
                self.fname.append(column[0])
                self.lname.append(column[1])
                self.ID.append(column[2])
            del self.fname[0],self.lname[0],self.ID[0]
        def Tableformat(self):
            print "%-14s|%-11s|%4s" %('First Name','Last Name','ID')
            print "-" * 39
            for (i, fname) in enumerate(self.fname):
               print "%-14s|%-10s|%5s" %(fname,self.lname[i],self.ID[i])
                
    X = Student()
    X.Tableformat()
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  10. #9
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: python "no attribute "

    I don't know if you really know what def does, it specifically defines functions, so what you have there is a nested function. Nested functions are perfectly legal in Python, and can be used to great effect when you need to perform a similar mathematical function repeatedly on different sets of data. Again, in Python you do not have to explicitly define variables before they're assigned to, you simply assign to them and they spring into existence as needed. Def is only really good to define functions, so unless you need a function, don't use it. Also, with nested functions they don't need to be passed self.

    Second, whatever it is you're doing you're working too hard to do it. Python is more expressive than this, let me show you:
    Code:
            #make sure we read it right away
            self.datalst = []
            self.Reader = csv.reader(open("copy-john.csv", "rb"))
            self.datalst.extend(self.Reader)
            for column in self.datalst:
                self.fname.append(column[0])
                self.lname.append(column[1])
                self.ID.append(column[2])
            del self.fname[0],self.lname[0],self.ID[0]
    First, you don't need that Reader. Let's get rid of it:
    Code:
            #make sure we read it right away
            self.datalst = []
            self.datalst.extend(csv.reader(open("copy-john.csv", "rb")))
            for column in self.datalst:
                self.fname.append(column[0])
                self.lname.append(column[1])
                self.ID.append(column[2])
            del self.fname[0],self.lname[0],self.ID[0]
    Second, you don't even need to have that datalst object. You can make a list on the fly by using the built-in list() function:
    Code:
            #make sure we read it right away
            for row in list(csv.reader(open("copy-john.csv", "rb"))):
                self.fname.append(row[0])
                self.lname.append(row[1])
                self.ID.append(row[2])
            del self.fname[0],self.lname[0],self.ID[0]
    Finally, you don't need to delete that last stuff at the end of this. If you don't need the first rows, then simply exclude them from the for loop:
    Code:
            #make sure we read it right away
            for row in list(csv.reader(open("copy-john.csv", "rb")))[1:]:
                self.fname.append(row[0])
                self.lname.append(row[1])
                self.ID.append(row[2])
    There you have it. Simple, succinct, beautiful.
    Wow I changed my sig!

  11. #10
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    19

    Re: python "no attribute "

    Woo... Zeke. You are rock... I don't know how many years will it take me to learn all these things... Thanks a lot....
    This is my final project and we only have two classes on python, so yeah... Thanks.

    Any other suggestion so that I can become more expressive and confident with python? I know I am skipping around (that's the sad part).
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Alex G. CSS "Sexy Buttons" - what about "submit on 'ENTER'" ?
    By shackrock in forum JavaScript and CSS
    Replies: 7
    Last Post: 12-05-2010, 04:57 PM
  2. Creating a "dice" game using python
    By taabistan in forum Python
    Replies: 4
    Last Post: 12-01-2010, 02:31 AM
  3. "htop" style gui with python, how?
    By Symbolix in forum Python
    Replies: 3
    Last Post: 11-22-2010, 02:42 AM
  4. Replies: 2
    Last Post: 11-01-2010, 11:52 PM
  5. "ifs" and "elses" etc in python...help?!
    By amateur in forum Python
    Replies: 5
    Last Post: 07-10-2009, 01:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts