if statements in Python

by Trakis

Back to Mechanic's Corner.

Trakis2008-11-17 22:26:27
Most of my afflictions are handled pretty generically. I created an affliction object class that contains each affliction's priority, cure methods, and such.

For each of the 'special' afflictions, though, like Aeon, Blackout (discussed in my other thread) have their own cure functions, like cureAeon(). So I'm thinking of the best way of calling these functions.

Two thoughts so far:

1. Have a different cure function for each special affliction. Ex: cureAeon(). Execute the code with exec(). I'm not sure how good this is, in terms of coding style.
2. Have a single cureSpecial(aff) function, which then calls the function using a series of if/elif statements.

if aff == "aeon":
cureAeon()
elif aff == "special2":
cureSpecial2()
...etc.

Is one method faster than the other, or better than the other in some way?
Unknown2008-11-17 22:49:48
There as many ways to write a combat system as there are afflictions in Lusternia. Whatever works for you, such that you understand it, it works, and you can maintain it, is a good solution in my book.
Daganev2008-11-17 23:59:56
Long if checks are always slower. Is there switch statement you can use instead?
Trakis2008-11-18 02:01:49
Kiind of. The closest thing is a lambda function. You can also build a dictionary that calls certain functions depending on what the "key" you provide is. Most of the language is designed to be as readable as possible - a feature that I love.
Unknown2008-11-18 03:06:23
It's been a while since I coded anything in Python, but I'm sure you could build a lookup table with lambda functions (or just function pointers) to do your healing.

And, in my experience, Python is less readable than a few other script languages. Perl is one of the least readable that I've used, and I'd classify Python as the second least. Lua's the most readable one I've used yet. smile.gif
Unknown2008-11-18 10:16:46
QUOTE(Zarquan @ Nov 18 2008, 04:06 AM) 583397
It's been a while since I coded anything in Python, but I'm sure you could build a lookup table with lambda functions (or just function pointers) to do your healing.

And, in my experience, Python is less readable than a few other script languages. Perl is one of the least readable that I've used, and I'd classify Python as the second least. Lua's the most readable one I've used yet. smile.gif


Funny, I've found the opposite to be true. What I absolutely adore about python is that I don't have to worry about brackets (for the most part). That imo makes every code unreadable even if you space nicely. I have a much easier time holding together what goes where in python.

I also use simplistic 'functional' curing + lists / dicts.. which I think most people do. I've been wondering about creating affliction objects but I can't figure out a structure that wouldn't simply make it ineffective (as shown above by Trakis example).
Unknown2008-11-18 12:05:15
The stuff I saw from Keldar in Achaea was all class-based, and he was doing a few unorthodox things that made it look very complex and almost unreadable. He's since switched over to Lua, so I can't show you the code to which I'm referring, unfortunately.

At any rate, here's a site I might recommend for you to quickly and easily learn a few of the more advanced tricks in Python. The entire book is available online (or you can download different formats to use offline).
Trakis2008-11-18 13:57:15
Well, the design philosophy of Python matches what most people say about Lua. Both emphasize readability. Hence that quote somewhere about how, "Python is executable pseudocode... Perl is executable line noise", or something like that.

shadow: Most of my stuff is fairly simple too. Just regular functions, lists, dictionaries. I try to code in a way that makes my code readable, though.

Zarquan: Thanks for the site. I'll take a good look at it later.
Unknown2008-11-18 14:08:22
QUOTE(Trakis @ Nov 18 2008, 02:57 PM) 583557

I try to code in a way that makes my code readable, though.


"Real programmers don't comment their code. It wasn't easy to write so why should it be easy to read?"

XD

(No clue whom that quote is from, but it was too good to pass up upon.)
Daganev2008-11-18 18:53:05
...

How and why do brackets make it harder for you to read code? It makes it much easier for me.
Trakis2008-11-18 19:15:48
Perhaps what could be said, is that:

Readability of code that uses brackets depends on, to a great extent, how these brackets are used.
Daganev2008-11-18 20:32:47
well she specifically said "even if you space nicely."
Unknown2008-11-18 21:56:57
I would much rather use (properly aligned) brackets to denote scoped blocks than be forced to indent my code in a certain way. tongue.gif
Trakis2008-11-18 22:16:51
I love the use of whitespace. The only time that's an issue is it you're coding on Linux, I think.
Unknown2008-11-18 23:15:49
Whitespace has its uses, but I still prefer Lua. biggrin.gif
Daganev2008-11-18 23:26:28
Unknown2008-11-18 23:27:56
Apples and oranges there, friend.
Daganev2008-11-18 23:34:01
its the only time I can see whitespace having a use tongue.gif

invisible code is just not very usefull.
Trakis2008-11-18 23:53:17
Back on topic, I'm going with lambda. =)
Unknown2008-11-19 03:03:24
Lambda functions are fun and very powerful when used correctly. Good luck!