Could somebody _PLEASE_ explain what's going on here?

Mike Fletcher mfletch at tpresence.com
Fri Nov 3 20:14:22 CET 2000


print is asking your class for the attribute/method __str__, if it got an
attribute error there it _then_ would ask for __repr__, since you're
returning None as __str__, you're getting nowhere.  Here's a simple
solution:

	def __repr__( self ):
		...
	__str__ = __repr__

HTH,
Mike

-----Original Message-----
From: Steve Juranich [mailto:sjuranic at condor.ee.washington.edu]
Sent: Friday, November 03, 2000 1:36 PM
To: python-list at python.org
Subject: Could somebody _PLEASE_ explain what's going on here?


Okay, I'm getting really frustrated here because I don't understand what's
going on.  Up to this morning, I had a class called "Table" that responded
very nicely to the "print" command.  Now, all of a sudden, it doesn't work. 

I have made a change in the code.  I overloaded the '__getattr__' to the
following:

    # 
    # 
    # This deals with accessing options that may have not been defined
    # in the options file.
    def __getattr__(self, k):
        try:
            return self.__dict__[k]
        except KeyError:
            return None

and now here's my __repr__ function:

    # 
    # 
    # This is mainly used for debugging.
    def __repr__(self):
        strn = ''
        for k in self.__dict__.keys():
            if k == 'num_entries':
                continue
            strn = strn +  k + " : "
            if self.__dict__[k] == 1:
                strn = strn + 'True\n'
            elif self.__dict__[k] == 0:
                strn = strn + 'False\n'
            else:
                strn = strn + str(self.__dict__[k]) + '\n'

        # Get rid of the last carriage return (print adds one of its
        # own).
        if strn[-1] == '\n':
            strn = strn[:-1]
        
        return strn

The net result is I now get the following behavior:

Python 1.6 (#4, Sep 21 2000, 16:30:17)  [GCC 2.95.2 19991024 (release)] on
sunos5
Copyright (c) 1995-2000 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>> import Table
>>> t = Table.Table('parfile.peaks')
>>> print t 

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: call of non-function (type None)
>>> print `t`
input_file_list : first_list
testing : True
output_dir : First
no_coda : True
use_energy : False
accent_constraint : none

As you can see, "print t" pukes, while "print `t`" works.  Why is this
broken now?  How do I fix it?

Thanks for the help.

Can't-think-of-a-pithy-sign-off-ly y'rs,

----------------------------------------------------------------------
Stephen W. Juranich                         sjuranic at ee.washington.edu
Electrical Engineering         http://students.washington.edu/sjuranic
University of Washington             http://rcs.ee.washington.edu/ssli


-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list