What's so good about Python?
Printable View
What's so good about Python?
what is python :confused:
It's an embeddable scripting language. I found no use for it whatsoever.
Z.
Apparently it is a good language to start learning, but I say BS to that. :D Just start with something strictly OO, such as C++ or Java. VB is not a good first language. :(
Ruby is an OO scripting langauge that's supposed to be a better Perl than Perl. I intend to play with that or Python eventually...
I heard once that Python has a special data type that lets you use numbers potentially thousands of bits long! So you could evaluate Pi to a lot of decimal places, for instance.
That's the only interesting thing about python so I hear! :)
I'll stick to VC++ or VB (imho, the nicest bits of kit that MS have ever produced).
It's used by scientists because you can have variables whose size/presicion is limited only by the amount of memory your computer has, so they can calculate π to millions of decimal places.
I spend about 90% of my time at work programming in Python, so I reckon I'm probably qualified to answer this a little better than most people :)
It is an embedable scripting language, as Zaei says... although that's just one side of the things you can do with it.
You can embed Python into a C/C++ application, which essentially just passes your Python commands to the Python interpeter. It's not hard to do, and has been used in a couple of commercial games, such as Severance: Blade of Darkness (published by Codemasters). You can also extend Python with modules written in C/C++. So it is both embeddable and extendable.
Python is also very much an object-oriented language, contrary to what some people in this thread seem to think.
The main reason people who use Python use it is that it's pretty easy. It's an interpreted language so it doesn't cause catastrophic errors when things go wrong but instead gives you a useful traceback, containing information about where and when the error happened and how the flow of the program arrived there.
There are many different modules that you can import to do things that are pretty complicated in many other languages. For instance, to get a web page in code: (hash signs are comments in Python)
That's basically 2 lines of code, not including importing the module containing the utilities (a bit like a #include in C/C++).Code:#urllib is a module with FTP/HTTP utilities in it
import urllib
#now urllib is imported, we can use stuff that's in it
#get a page and assign it to a variable
vbforumshomepage = urllib.urlopen("http://www.vbforums.com")
#urlopen returns a file object so vbforumshomepage is a file object
#print the HTML for the page
#file objects have a read method which returns their contents
print vbforumshomepage.read()
As Wynd says, it's often used for scientific purposes because it's very good for prototyping mathematical models and has native support for theoretically infinitely long numbers and for complex numbers. It gets a fair bit slower when you're dealing with numbers that aren't in the usual 32-bit range, but it's easy to do if you don't mind the performance hit. You'd get the same performance hit in any other language anyway. It's also platform-independant, so a researcher can get a number-crunching app working on their laptop/desktop, then set it running on a big Solaris server (or whatever) without any code changes.
It just generally has handy built-in types, like lists and mappings. Very useful stuff. Humans basically think in terms of mappings, so having them built-in makes programming easier. I think there are STL map classes in C++ but I haven't really investigated them much.
If you are interested in using something like Python as an embedded scripting language for games, I have heard some good things about a language called Lua (www.lua.org) which is similar to Python. Baldur's Gate was scripted in Lua.
Also, if you're familiar with C/C++, Python should be very easy to pick up since it has similar styles.
My only gripe with Python at the moment (currently stable version 2.1 I believe) is that the typing is slightly too loose. It's fine with the built-in types, but class typing is a little awkward.
Anyway, if anyone has any questions about Python, I can probably answer them but if they stump me I have some very experienced people available to me that I can ask.
Ill bet my scripting language beats out Python in speed =).
Sounds pretty interesting, Harry. Thanks for all of the info. I downloaded it once, but never really got into it, no time (that seems to happen a lot, these days =).
Z.
Well Zaei, if your scripting language does what Python does better and faster, lets see it :)
I did say speed =).
Its a psudo ASM thing (twice as fast as VB =). I would like to get around to adding structs, and perhaps classes, but i dont have the time at the moment.
Z.
it's pseudo, not psudo Zaei ;) May I ask you how you perform the speed tests, because it's probably biased, it also depends on what exactly is performed and Python/VB probably has wide range of capabilities. Pretty much your tradeoff of performance is the introduction of a higher paradigm such as OOP, and to minimize it you have to optimize the compilation which isn't possible for a scripted language.
A for loop, performed 1000 times with this code:
the C++ versionCode:x = 1
x = x + 2
debug.print x
x = x -1
debug.print x
x = 10
x = x * 2
debug.print x
x = x / 2
debug.print x
my version:Code:x = 1;
x = x + 1; // might have been x += 1, but I dont remember
cout << x << endl;
x = x - 1;
cout << x << endl;
x = 10;
x = x * 2;
cout << x << endl;
x = x / 2;
cout << x << endl;
The only place it might be biased is that my script is contained inside of a C++ for loop, but it also includes the need to Reset every execution. So it is a bit biased =).Code:mov aa 1
add aa 2
push aa
int 0 // same as "cout << x << endl;"
sub aa 1
push aa
int 0
mov aa 10
mul aa 2
push aa
int 0
div aa 2
push aa
int 0
And kedaman.... everyone knows that I cant type =).
When I get home, I will rewrite the test to use a scripted for loop (maybe I should change the mul aa 2 to a shl aa 1 for some more speed =). We shall see =).
Z.
Oh, and it IS compiled, kedaman =). Thats what makes it so fast. The only interpretation that needs to be done is to determine if the second set of data to an opcode is a variable or actual data =).
Z.
debug.print lol, cout, nah, skip those, they're just hogging up all the performance flooding the actual results, do you compile to 80x86 or bytecode or something else?
Script wars, yeah! :)
Are you actually including those debug.print statements in your code Zaei? If so, I'm surprised it's only 2 times as fast. Take out all the input/output and leave it to the end.
Alright. I will create a new test. Gimme a few minutes =).
Z.
Uhhh... GetTickCount() isnt accurate enough to compare them. Both return 0ms =). Here is the test code:
Code:Dim i As Long
Dim x As Long
Dim y As Long
For i = 0 To 10000
x = 10
x = x * 2
x = x / 2
x = x + 1
x = x - 3
y = 10
x = x + y
y = x
Next i
Z.Code:sloop:
mov aa 10
mul aa 2
div aa 2
add aa 1
sub aa 3
mov ac 10
add aa ac
mov ac aa
inc ab
cmp ab 10000
jl sloop
Increase the number of iterations then :rolleyes: There is a more accurate counter too, I just don't remember the name. It works in microseconds I think.
How is your version significantly different from ASM?
Different bytecode, as well as some different opcodes (alloc, and la for arrays, etc).
Well, as it turned out, I screwed up the tests. I wasnt actually executing the script, simply loading it.
The really terrible part is that, when I fixed this little error, and compiled back to release mode, i get a VERY consistant 0ms exec time, for 1 million iterations. Im pretty sure that it does in fact execute the script, but I am making sure (even _I_ can hardly believe that!). Check back in a sec!
Z.
Arg. It still want executing. 1000ms for 100000 iterations, as opposed to vb, 100 =(.
Looks like I have some optimising to do. Have to shave off 900 ms =).
At least my 2 times faster then VB with Output still stands =).
Z.
Oh, it also has support for functions:
Code:function hello()
ret
call hello
Z.
doesn't matter, assembly sucks, no style ;)
::SLICE:: There goes about 870 ms. Its down to 120 - 130ms.
It's a different style, kedaman =). Besides, I gave it just a touch of style with function calls:
Z.Code:call main
int 0
end
function main()
mov aa 10
push aa
pop ab
mul ab 10
div ab 3
inc aa
inc ab
add aa ab
push aa
ret
Oh, in regards to all of the two letter variable names.... Its a speed thing. Each letter is one byte of a WORD that makes up a variable index in the program starting with aa, which is 0. I havent gotten around to adding nicer variable names into my preprocessor yet.
Z.
So uhh... why use your script instead of just ASM? Sorry, I thought one of the main ideas of a scripting language was that it was easy to write. As in high-level.
I thought it was portability :p and yeah without OOP a scripting language is pretty useless.
Its a component of my game engine. The design from the very beginning was to make it as modular as possible (the actual scripting module is standalone). Each script compiles into an Object, where each Opcode/lhs/rhs combo is 64 bits, plus function names and locations (so you can call script functions from engine code. This allows me to create Objects whose behaviors exist only as function pointers in some library or another, while scripts act as Builders, to construct a unique object. This would allow new game content (new units, weapons, etc) to be released, and plugged directly into the game.
Also, since the scripting language is at the lowest level, an HLL could be built right on top of it. Nothing limits it to pseudo(kedaman =) ASM.
Z.
Objects are an addition that I would like to add at some point. At the moment, though, it fulfills it's purpose nicely =).
Z.
And it is portable... the only thing that isn't really is the compiler (written in VB (oh the irony!) =).
Z.
You could write the compiler in gwbasic if you want, I don't care, probably the only thing that matters is the amount of hidden bugs inside the compiler when doing so. If i'm going to use any scripting for runtime purpose it's going to be python, but I hardly think so, because C++ is the ultimate tool for me, even for runtime scripting =) =) =) =) =) ;)
Just wondering: who uses Python the most, amateur programmers or professionals, is it a respected language yet (it's quite new isnt it?)?
Is it worth my while getting involved with it (I'm an unemployed programmer trying to diversify into other languages, like C)?
Well to be honest there probably aren't a great many places that use Python, not because it's not useful to them but because it doesn't have as much support as something in a comparable position like Java or Perl (both similar in ways to Python). It's not all that new. If you want to know how long it's been around, visit www.python.org.
If you're a programmer looking for useful languages to learn and you don't know C already then I would definitely recommend you learn C/C++. It's really an excellent way to get a better feel for the way the computer works at nearly the lowest level, which will ultimately (IMO) make you a better programmer overall.
Python is really a great, flexible high-level language, but unless you are lucky enough to get to work with an open-minded organisation that really looks at all the technologies available seriously (ie not a M$ shop), you will probably not get to use it much.
If you want to field questions like this to a group of Python programmers, try the main Python IRC channel - irc.openprojects.net #python. I'm in there fairly often, along with #zope which also runs on that server, as HarryW.
Is there any (easy) way to embed Java into another (non-Java) application?
Z.
I don't really have any idea. You're probably better off asking that in the Java forum :)
Its just curiosity =). Both Java and Python are scripting languages, so I thought id just ask =).
Z.
Well, although the official Python project is written in C, there is another ongoing project to implement it in Java (called Jython I think), and that's got to be embeddable, so it must be possible.
For whoever asked about accurate timers...
The Pentium introduced the RTDSC (or is it RDTSC?) instruction that gets the number of clock cycles (i.e. the time is dependent on processor speed) since the processor was initialised (I think this is only on power-up, but may be reset on a hard system reset).
Either way, most assemblers won't support it :( MSVC lets you use the _emit pseudoinstruction that DIRECTLY includes the bytes you specify into the output object file.
I did post an example on the forum...
Accurate timers...
I was browsing the DJGPP C++ compiler's help files a few weeks ago and it said that it has a function that is accurate to +/- 840 nanoseconds (8.4*10^-9 seconds) !
I don't know if anyone can verify this, but its interesting anyway.
Any thoughts?
Try the QueryPerformanceCounter API, it is quite accurate. On my computer it has a frequency of 3579545 times a second.
But it's not quite 500000000 times a second, is it? ;)Quote:
Originally posted by TiPeRa
Try the QueryPerformanceCounter API, it is quite accurate. On my computer it has a frequency of 3579545 times a second.