Could somebody _PLEASE_ explain what's going on here?
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
def __repr__( self ):
__str__ = __repr__
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
# This deals with accessing options that may have not been defined
# in the options file.
def __getattr__(self, k):
and now here's my __repr__ function:
# This is mainly used for debugging.
strn = ''
for k in self.__dict__.keys():
if k == 'num_entries':
strn = strn + k + " : "
if self.__dict__[k] == 1:
strn = strn + 'True\n'
elif self.__dict__[k] == 0:
strn = strn + 'False\n'
strn = strn + str(self.__dict__[k]) + '\n'
# Get rid of the last carriage return (print adds one of its
if strn[-1] == '\n':
strn = strn[:-1]
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
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.
Stephen W. Juranich sjuranic at ee.washington.edu
Electrical Engineering http://students.washington.edu/sjuranic
University of Washington http://rcs.ee.washington.edu/ssli
More information about the Python-list