[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] "Try" For World Loading



On Sunday 20 November 2005 06:16, Kris Schnee wrote:
> with names determined by coordinates. If the player moves outside a
> zone, the game will look for a zone with a certain name to load it. But

"certain"?

is the zone generally is supposed to exist?

> the file for that zone might not exist. I'm about to write the function

.. or is it normal that it does not?

i guess if it e.g. just increments an index (the x/y/z location for example) 
it can be quite a normal case that such zone does not exist -- worlds do tend 
to have limits.

> so that it uses the "try" statement to look for the file. Is that bad
> for some reason, or even "bad form?" Is there a reason to do things a
> different way, namely looking at a list of all files and saying "if
> desired_filename in list_of_files, load?"

if i'd have a list (or a dict for that matter), i'd probably do a if-in with 
it.

for when to use exceptions (i.e. things that try-except catches) i think the 
samurai principle is a nice one - that considers exceptions (not returning) 
the right way to handle failures. i guess it depends on the context a lot 
which is a sensible 'failure' .. like are for-loops (iteration) stopped with 
an exception when e.g. the list reaches end? that is in a way expected when 
iterating a list, but otoh also an 'exception' in the getting of the next 
list item .. or something.

then again, as that 'look before you leap' text posted in the earlier reply 
reminded, even if you check if os.path.exists you still may need to use 
try-except .. at least if the filesystem is not to be trusted (sometimes 
people use network shares etc.)

> Kris

~Toni