Hard Light Productions Forums
Off-Topic Discussion => Programming => Topic started by: Kopachris on January 14, 2013, 07:04:29 am
-
Okay, I'm freaking loving Python right now. It seems so... elegant. What really won me over is that I can do something like this extremely easily:
class HeaderChunk:
...
class TextureChunk:
...
...
chunk_dict = {
b'HDR2': HeaderChunk,
b'OHDR': HeaderChunk,
b'TXTR': TextureChunk,
...
}
while not eof:
chunk_id = pof_file.read(4)
this_chunk = chunk_dict[chunk_id]()
...
Without the filler ellipses, of course.
Way prettier than a bulky C-style switch-case.
Anyone around here dabble in Python?
-
Kinda reminds me some xkcd's posts.
-
Kinda reminds me some xkcd's posts.
import antigravity
will open the user's default browser to this page (http://xkcd.com/353/) in any standard Python distribution.
-
Anyone around here dabble in Python?
Aye, learned it for work. Nothing overly complicated so far, but I did write a partially-structured 4-D interpolation routine at the end of my first week with Python :)
To me, the main advantages of Python are its open-sourceness (as opposed to MATLAB, the main competitor here at work) and the fact that you don't need to compile it (which saves a lot of time in bugfixing and general messing about).
As for the simplicity of Python coding... Idk, I have a feel that C++ can be just as simple, as long as you don't go hacking its object-oriented paradigm with pointers and stuff. A lot of more experienced coders than me seem to be doing this, and I still haven't got a clue why.
-
Lolwut... you can't do **** in C++ without pointers. Well, you can, but you'd be stupid to avoid using pointers :blah:
-
OOP has nothing to say about pointers. In C++, they're kinda essential when trying to do polymorphism, IMHO.
Also, please remember that C++ has two ways of dealing with pointers, either via the C-style kind ( int* intpointer) or via C++ references ( void myFunc(int& a) {}).
-
Lolwut... you can't do **** in C++ without pointers. Well, you can, but you'd be stupid to avoid using pointers :blah:
In Python, basically everything is pointers. For example:
foo = [0,1,1,2,3,5,8]
bar = foo
bar.append(13)
print(foo)
>>> [0,1,1,2,3,5,8,13]
Really useful if you know about that feature. Really annoying if you don't.
-
In C++, they're kinda essential when trying to do polymorphism, IMHO.
I see... Never got that far with C++ :)
In Python, basically everything is pointers.
One of the things I find annoying in Python is that because of this, you can pass basically anything to a function, there's no way it will go "Hey I didn't expect that data type in that position". Which can make for some pretty damn obscure bugs...
-
lua has that problem too, and no seemingly fast way to verify data types. the surefire way is the type command, but then you have to do a string compare. you can check for nil, but then you have to worry about the wrong datatype. so you have to choose pretty early on to either make your function safe or fast. the other side of it is you can make functions more versatile. when i do classes i usually have one ultra-versatile constructor, and then a bunch of fast ones.
i never bothered learning pyhon. so far i can do everything i need to do with standalone lua.
-
In Python, basically everything is pointers.
One of the things I find annoying in Python is that because of this, you can pass basically anything to a function, there's no way it will go "Hey I didn't expect that data type in that position". Which can make for some pretty damn obscure bugs...
def Foo(x):
"""Creates a 2D array of length x."""
if not isinstance(x, int):
raise TypeError("Expected integer!")
bar = [[] for i in range(x)]
return bar
or
def Foo(x):
"""Creates a 2D array of length x."""
x = int(x) # Will raise exception if x can't become int
bar = [[] for i in range(x)]
return bar
Annoying to have to do that, I agree, but it's not hard. I usually skip it and just keep track of my types on my own.