#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    15
    Rep Power
    0

    Wired error " __init__() should return None"


    Hi guys,
    I created a code which takes a file (which is a table/chart )that looks like this:
    Name,Gender,Age,Salary,Height
    Andrew,M,21,4000,175
    John,M,43,8000,182
    Anna,F,24,6000,166
    Ruth,F,32,6500,172

    and returns a nested list of its data. in other words:
    M,21,4000,175
    M,43,8000,182
    F,24,6000,166
    F,32,6500,172

    this is my code:

    Code:
    class Table:
        
        def __init__(self,path,sep):
            self.path=path
            self.sep=sep
            
            try:
                f=open(path,'r')
                g=[]
                read_file=f.read()
                split_file=read_file.split()
                for line in split_file:
                    list_the_line=line.split(sep)
                    g.append(list_the_line)
                g.remove(g[0])
                for i in range (len(g)):
                    g[i].remove(g[i][0])
                f.close()    
                return g    
        
                
                
            except ValueError:
                print "cannot creat a table object from this file"
                return
    table = Table('table_example.txt',",")
    print table
    I didn't finish the code ( I have yet to write cases in which the object will return errors, and which cases are errors)
    but it should work, but for some reason it sends me this error :

    Traceback (most recent call last):
    File "C:\Users\Lior\Desktop\hw\hw7 part1.py", line 27, in <module>
    table = Table('table_example.txt',",")
    TypeError: __init__() should return None

    I don't even know what does it mean,
    could you please explain to me?
    thank you for your time.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    Loose the return g. Note that "print table" will print something like "class instance". It is not your variable but the instance of the class Table that you created. You have to study classes a little more. To solve the problem you would use an instance object/variable.(what is self) Also this code will not work as expected if there are any spaces in the data. Take a look at using readlines instead. This code is not tested (we don't have any data) but is meant to be a general guide.
    Code:
    class Table:
        
        def __init__(self,path,sep):
            self.path=path
            self.sep=sep
            
            try:
                f=open(path,'r')
                self.g=[]
                read_file=f.read()
                split_file=read_file.split()
                for line in split_file:
                    list_the_line=line.split(sep)
                    self.g.append(list_the_line[1:])      ## remove the first item
                self.g.remove(g[0])
                """                                              handled by [1:] above
                for i in range (len(g)):
                    g[i].remove(g[i][0])
                """
                f.close()    
                ##return g                                           ## removed
        
                
                
            except ValueError:
                print "cannot creat a table object from this file"
                ## added as you muffle the error message with the print statement
                raise       
                ##return                                         ## not necessary
    
    table = Table('table_example.txt',",")
    print table.g                                             ## changed
    Last edited by dwblas; December 7th, 2013 at 11:40 AM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    class Table:
        def __init__(self,path,sep):
            try:
                g=[]
                return g
    class constructors must return None. In some cases you return a list.

    ps. I really truly had hoped for "wiring" as this thread's subject.
    Last edited by b49P23TIvg; December 7th, 2013 at 12:15 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo