[Tutor] Odd AttributeError

Neil Hodge neh@attbi.com
Wed May 21 11:40:02 2003


LLoyd:

On Wed, 2003-05-21 at 08:16, Lloyd Kvam wrote:
> The secret could be in the lines you left out.  file is not a good
> variable name since it is also a python type.  

Undoubtedly.  This is someone else's code that I am modifying; I will
change the "file" variable name.

> Your code works:
> (i changed file to filex)
> 
> class ID3:
>       def __init__(self, filex, name='unknown filename', as_tuple=0):
>           print 'printing as_tuple:', as_tuple
>           print 'defining self.as_tuple . . .'
>           self.as_tuple = as_tuple
>           print 'printing self.as_tuple: ', self.as_tuple
> 
> filex = file(r'c:\autoexec.bat','rt')
> ID3(filex)
> 
>   >>> printing as_tuple: 0
> defining self.as_tuple . . .
> printing self.as_tuple:  0
> 

Interesting; apparently your comment about the problem being elsewhere
is the case.  OK, here is all of the code in that class up until that
point:

class ID3:

    genres = [ 
    "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", 
    etc. . . .
    ]

    def __init__(self, f, name='unknown filename', as_tuple=0):
        if type(f) in string_types:
            self.filename = f
            # We don't open in r+b if we don't have to, to allow
read-only access
            self.f = open(f, 'rb')
            self.can_reopen = 1
        elif hasattr(f, 'seek'): # assume it's an open file
            if name == 'unknown filename' and hasattr(f, 'name'):
                self.filename = f.name
            else:
                self.filename = name

            self.f = f
            self.can_reopen = 0

        self.d = {}
        print 'printing as_tuple:', as_tuple
        print 'defining self.as_tuple . . .'
        self.as_tuple = as_tuple
        print 'printing self.as_tuple: %d ' % self.as_tuple

And here is the output:

printing as_tuple: 0
defining self.as_tuple . . .
Traceback (most recent call last):
  File "./id3-tagger.py", line 93, in ?
    main()
  File "./id3-tagger.py", line 68, in main
    id3info = ID3(f)
  File "./ID3.py", line 212, in __init__
    print 'printing self.as_tuple: %d ' % self.as_tuple
AttributeError: ID3 instance has no attribute 'as_tuple'

Frankly, I do not see anything prior to the error in the code that would
contribute to this, but obviously something is going wrong.

Let me know what you think.

Neil