I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can show if how you were doing this in C++ and how this is done in Python.
OK i think i did not put the question heading or question right, basically i was confused about the "Main" Function, otherwise other things are quite obvious from python official documentation except this concept.
Thanks to all
Excellent answer, but none points out what I think is one key insight for programmers coming to Python with background in other languages such as Java or C++: import
, def
and class
are not "instructions to the compiler", "declarations", or other kind of magical incantations: they're executable statements like any other. For example, the def
statement:
def f(x): return x + 23
is almost exactly equivalent to the assignment statement:
f = lambda x: x + 23
(stylistically the def
is preferable as it makes f.__name__
meaningful -- that's the "almost" part; lambda
is rather limited and should only ever be used when you're really keen to make an anonymous function rather than a normal named one). Similarly,
class X(object): zap = 23
is equivalent to the assignment:
X = type('X', (), {'zap': 23})
(again, stylistically, class
is preferable, afford more generality, like def
it allows decoration, etc, etc; the point I'm making is that there is semantic equivalence here).
So, when you run a .py
file, or import it for the first time in a program's run, Python executes its top-level statements one after the other -- in normal good Python style, most will be assignments, def
, class
, or import
, but at least one will be a call (normally to a function) to execute that function's body of code (def
, like lambda
, just compiles that code; the compiled code object only executes when the function or lambda is called). Other answers have already suggested practical considerations such as testing __name__
in order to make a module that can either be run directly or imported, etc.
Finally, it's best to have all "significant" code in functions (or methods in classes), not just stylistically, but because code in a function executes significantly faster (since the Python compiler can then automatically optimize all accesses to local variables). For example, consider...:
import time
lotsotimes = range(1000*1000)
start = time.time()
for x in lotsotimes:
x = x + x
stend = time.time()
print 'in module toplev: %.6f' % (stend - start)
def fun():
start = time.time()
for x in lotsotimes:
x = x + x
stend = time.time()
print 'in function body: %.6f' % (stend - start)
fun()
On my laptop, with Python 2.6, this emits:
in module toplev: 0.405440
in function body: 0.123296
So, for code that does a lot of variable accesses and little else, running it in a function as opposed to running it as module top-level code could speed it up by more than 3 times.
The detailed explanation: at module-level, all variables are inevitably kept in a dictionary, so each variable access is a dict-access; local variables of a function get optimized into a special array, so access is faster (the difference is even more extreme than the 20% or so speed-up you'd see by accessing an item in a Python list vs one in a Python dict, since the local-variable optimization also saves hashing & other ancillary costs).
When you run a script through the Python interpreter (or import that script from another script), it actually executes all the code from beginning to end -- in that sense, there is no "entry point" to a Python script.
So to work around this, Python automatically creates a __name__
variable and fills it with the value "__main__"
when you are running a script by itself (as opposed to something else importing that script). That's why you'll see many scripts like:
def foo():
print "Hello!"
if __name__ == "__main__":
foo()
where all the function/class definitions are at the top, and there is a similar if-statement as the last thing in the script. You are guaranteed that Python will start executing the script from top-to-bottom, so it will read all of your definitions there. If you wanted, you could intermingle actual functional code inside all the function definitions.
If this script was named bar.py
, you could do python bar.py
at the command line and you would see the script print out "Hello!"
.
On the other hand, if you did import bar
from another Python script, nothing would print out until you did bar.foo()
, because __name__
was no longer "__main__"
and the if-statement failed, thus foo
was never executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With