r/learnpython • u/puremath369 • Jun 10 '17
Resource for transitioning from Java to Python? Don't really understand the reason for beginning code this way:
Watching this series on deep learning (thought I could learn python alongside it) but I'm confused why he starts the code with this "if name = 'main':" line. Anyone know of any resources that just quickly compare how to transition from java to python?
2
u/Pinakanakapagpapabag Jun 10 '17
When the Python interpreter reads a source file, it executes all of the code found in it.
Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special name variable to have a value "main". If this file is being imported from another module, name will be set to the module's name.
In the case of your script, let's assume that it's executing as the main function, e.g. you said something like
python threading example.py on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that name does equal "main", so it will execute the block shown there.
One of the reasons for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.
https://stackoverflow.com/questions/419163/what-does-if-name-main-do
1
u/puremath369 Jun 10 '17
Thanks so much! Like another user suggested, I guess I should just learn python from the ground up :/
1
u/Pinakanakapagpapabag Jun 10 '17
Yeah, I'm a bit cynical as a former educator about "best method to learn x" statements. We all have different learning strategies we are accustomed to as well as new ones that might be able to help us if learn to employ them (or employ them more effectively). Comparing languages isn't always fruitless, and sometimes teaches you new things. For example, I google what's the equivalent of language x's method in language y often get's me useful results even when there is no comparable method per se as there is almost always some approach to do the same thing even if it takes more code.
20
u/K900_ SRE person // please don't DM me your questions Jun 10 '17
Since Python doesn't really have the concept of a single entry point, when you run a .py file, it's simply interpreted line by line, and if you have top level statements in it, they're executed. But you can also import the same .py file as a module, and it also gets interpreted line by line.
__name__ is a magic variable that's set by the interpreter. If your module is executed as the "main" module, it's set to the magic value of __main__. If it's imported from another module, it's set to the name used to import your module.
The explicit if __name__ == "__main__" check is there so that if you run the script directly, the main function is executed, but you can still import it as a module as well.
1
u/michaelherman Jun 10 '17
Python for Java Programmer Resources:
Help with if __name__ == "main":: https://github.com/mjhea0/thinkful-mentor/tree/master/python/main_routine
2
2
u/CGFarrell Jun 10 '17
As a TL;DR, the if name thing checks if the file is being run as a script or being included elsewhere. So I can have my classes in a file, and I can run the file to perform my if main stuff, or import it to get the classes.
0
Jun 10 '17
[deleted]
3
u/KleinerNull Jun 10 '17
Since python isn't OOP
Since when?
1
u/Snaketooth10k Jun 10 '17
Yeah I'm not tracking. Python is ABSOLUTELY good for OOP. Reusability inheritance and abstraction are pretty much what python is all about. Even Wikipedia states it's a multi-paradigm language which includes OOP.
2
u/KleinerNull Jun 10 '17
Yes, python doesn't force you to code object orientated, but still the language itself is heavily object orientated. Everything besides statements is an object. Functions/methods even modules are objects.
So in my mind you can't just say python isn't OOP. Even if you don't need to code OO.
1
u/M123Miller Jun 10 '17
Yeah I did that for my undergrad thesis. Went from an OOP background with basic geometry and chose to do Python ML. I learnt a lot and Python was the easy part but still... I scraped a 68 for the project.
1
u/KubinOnReddit Jun 10 '17
Everything in Python is an object
"Nah, not OOP."
If a language is multi-paradigm it doesn't mean it isn't OOP anymore.
2
u/xiongchiamiov xiongchiamiov Jun 10 '17
Also, java isn't truly object-oriented; there are still a bunch of non-objects in it, like integers and functions. Smalltalk and Ruby buy into objects much more.
26
u/TheChance Jun 10 '17
I'd discourage you from thinking in these terms. Pick up Python from zilch, and work your way through some exercises. Programming languages are tools, not activities, and if you're thinking about "transitioning" from one language to another, you're stuck in a box.
Python represents, basically, the top-down perspective to a C-style language's bottom-up perspective. I promise that after a few hours of working your way through the basic stuff, it will all just click. I expect you'll fall in love with it, actually, because it's gonna seem like you're speaking to the computer in plain English by comparison to Java or C++.
But, yeah, don't look for "Python for Java programmers." That's a fool's errand. It's like looking for a book titled, "Jigsaws for table saw users."
Less obtuse explanation of that specific thing, to reiterate the above:
if __name__ == "main":is equivalent to your main() function in Java. The big difference is that the same .py file can also be a library.If you run the file -
python myfile.py- it will begin running the code under that check, exactly like main() runs. But if you import the file, rather than running it, that "main function" might as well not exist.One naive use for this might be testing a library while you're writing it. That's not a naive use of main, it's a naive way to run tests =P but there's an example of the flexibility we're talking about.