r/learnpython • • 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?

35 Upvotes

16 comments sorted by

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.

1

u/puremath369 Jun 10 '17

So start all over? Shucks, I was hoping to avoid doing that lol. I figured all OOP languages would be relatively similar enough to only need to learn their subtle differences

8

u/TheChance Jun 10 '17

Well, they are and they aren't, and you're not starting all over. This is the big thing most newer coders struggle to accept. Programming is the activity. You're not writing Java, you're writing in Java. It's just like human language in this regard; one doesn't write English, or write Spanish, one writes in English or in Spanish. The writing is the activity, the communicating of information.

Programming is the same. From language to language, you're still just communicating data with and between computers. Just like with spoken language, the lexicon and the syntax might be entirely different, but you can always communicate more or less the same information one way or another.

The analogy breaks down here because learning a new programming language is nowhere near as challenging as learning a new spoken language, for exactly the reason illustrated above: you're communicating the same information in a somewhat different way, but unlike spoken languages, they're only so different.

C-style languages (such as C, C++, Java and C#) all have a great deal in common in terms of syntax and methodology. So-called "scripting languages" - higher-level languages like Python or Ruby - also have a great deal in common, and I think you'll be relatively comfortable with them, because they're, yeah, a lot more like coding in English. There was a macro on the front page of /r/python a few days ago, the Python logo, and superimposed, something like, "You can't run pure pseudocode" "Challenge accepted." I don't remember the actual text, but hopefully you get the point.

So it's still gonna be OOP underneath. You're gonna go through Hello, world! and some math problems and some function problems and etc. but you aren't learning to use those things again, you're just learning to use them in Python.

Python can be 100% OOP if you want it to be. Just gotta learn to use it its way, rather than trying to figure it out in terms of Java.

The first few days, you're gonna realize that you know all this stuff, but it's just somehow cleaner in Python. Not every tool is right for every job, but this language is a very versatile (and ubiquitous) tool that I think any budding programmer should be familiar with.

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.

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

u/[deleted] 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.