PDA

View Full Version : I would like to make some logic out of this (Python)


rui_mac
10-07-2010, 10:50 AM
I know what it is supposed to do, but...
What is this __name__ variable? Where does it come from? Why do I need a conditional to run my main procedure?

if __name__=='__main__':
main()

donelgreeko
10-07-2010, 11:17 AM
Hi Rui,

when you write a script and it's a standalone but should also work as a module (because it offers some cool functions that other scripts can use too), you can use this if-cause to ensure that main() is not executed when the script file is imported by another script. In the most cases this is not really necessary for C4D scripts and plugins, but its a habit and looks a little bit more pythonic.


def HelperFunction1():
pass

def HelperFunction2():
pass

def main():
HelperFunction1()
HelperFunction2()

if __name__=="__main__":
main()


Cheers, Sebastian

rui_mac
10-07-2010, 01:56 PM
I understand your explanation (thank you) but I still miss the logic of it.
Where does the __name__ variable came from? It is generated automatically? What does it hold? The name of the procedure that is currently running? And if so, why is it called "__main__" and not just "main"?
Also, in your example, where do I create the actual code for HelperFunction1 and HelperFunction2? They just seem like definitions but they have no code in them, just a pass (I assume it serves for the compiler to "pass" through them, ignoring them).
So, when you call them from the main, what exactly gets executed?
I know these are lots of questions but I have been programming for a looooong time and all this seems a bit absurd. Python may be a powerful language but has lots of incongruences.

Rui Batista

donelgreeko
10-07-2010, 04:44 PM
In Python the keyword "pass" does simply nothing. Its a placeholder that the content is still missing and in my example I just used it because the content is not important. I could also write print "OK" instead . It's equal to void function{ /**empty**/ }. By the way, after the function header the parser expects an indention, so pass ensures this.

__main__ is automatically set by Python. If it would be "main" you would get a name collision with your function which might be "main".

Cheers, donelgreeko

rui_mac
10-07-2010, 04:49 PM
So, the __name__ variable does hold the name of the currently running procedure, right?
It is this indentation-base logic that annoys me ;-)
To me, a block of code is something between two keywords, like while... wend or if...endif or for...next for some languages or set by two characters, like { ... }.
A block of code defined by how many spaces are indenting the code is just way too weird ;-)

Rui Batista

donelgreeko
10-07-2010, 04:51 PM
So, the __name__ variable does hold the name of the currently running procedure, right?

It's the name of the script environment. Check this example:

MyTest.py

print __name__


MainScript.py

import MyTest #we import MyTest.py
print __name__


When you execute MainScript you get this output:

MyTest
__main__

Cheers, donelgreeko

rui_mac
10-07-2010, 04:53 PM
Oh, ok. Gotcha. Thank you :-)

Rui Batista

CGTalk Moderation
10-07-2010, 04:53 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.