S
Scott Brady Drummonds
Hi, everyone,
I'm a Python novice and would love some tips on how I should perform the
following task: I'd like to have my Python script read objects (using their
constructors) and terminate gracefully when an EOF is encountered. My first
attempt looked like this:
# This is enclosed in a 'try' block
file = open(...)
while 1:
# The Object constructor raises an exception for end-of-file
object = Object(file)
self.list = self.list + [object]
However, I'm not entirely comfortable having the Object constructor raise an
exception when the end-of-file is hit. Per my experience with C++, this
does not qualify as "exceptional behavior". Frankly, it is accepted. I'd
like to save the exceptions for true exceptions. My next guess:
# This is enclosed in a 'try' block
file = open(...)
while object = Object(file):
self.list = self.list + [object]
With this implementation, the file reading stops both with an exception
(raised by a parsing error, as an example) and when the Object constructor
returns something that makes the while conditional fail. However, therein
lies the rub: I don't know how to do this in Python.
Is there a way for me to make the second implementation work? Is there an
"empty" operator in Python that I could overload? Can I have a constructor
return 'null'?
Thanks!
Scott
I'm a Python novice and would love some tips on how I should perform the
following task: I'd like to have my Python script read objects (using their
constructors) and terminate gracefully when an EOF is encountered. My first
attempt looked like this:
# This is enclosed in a 'try' block
file = open(...)
while 1:
# The Object constructor raises an exception for end-of-file
object = Object(file)
self.list = self.list + [object]
However, I'm not entirely comfortable having the Object constructor raise an
exception when the end-of-file is hit. Per my experience with C++, this
does not qualify as "exceptional behavior". Frankly, it is accepted. I'd
like to save the exceptions for true exceptions. My next guess:
# This is enclosed in a 'try' block
file = open(...)
while object = Object(file):
self.list = self.list + [object]
With this implementation, the file reading stops both with an exception
(raised by a parsing error, as an example) and when the Object constructor
returns something that makes the while conditional fail. However, therein
lies the rub: I don't know how to do this in Python.
Is there a way for me to make the second implementation work? Is there an
"empty" operator in Python that I could overload? Can I have a constructor
return 'null'?
Thanks!
Scott