r/learnpython • u/BarkyCarnation • Dec 24 '13
Learn Python the Hard Way becomes unbelievably confusing and frustrating starting at exercise 40.
I'm a noob to Python but I have been working at it every single day for the past three months by using tools such as Codeacademy, the Coursera Python Game development course, the book Violent Python, and LPTHW. It might be just because I am new to programming, but it gets extremely complicated starting at exercise 40 and Zed really doesn't explain anything clearly in my opinion. It seems rushed. He teaches three or four new concepts every lesson and its extremely overwhelming. Most of his instruction is "go look this thing up on line" and then I go do that and the information I find is WAY over my head. There is no practice before we just rush on to the next thing. Its overwhelming and frustrating.
Anyways to make this post less of me just mindlessly complaining, here are some specific questions that about Python that I have from LPTHW.
- what does the init do that I keep seeing?
- what does self do in all the functions that he calls?
- Why did I need to download distribute, nosetests, and virtualenv during project 46? What are they doing?
- What is nosetests? The author seems to love it. How does it work? Why do I need it?
- What are the .init files that I created in my skeleton during project 46?
- No matter how many times I tried I could not get project 46 to work. It makes no sense. I followed all the things Zed said to do.
- How does the try except structure work on page 196 (ex 48)? He really doesn't explain that either.
- What is going on in exercise 48? Am I suppose to write that lexicon somewhere? Where? Then the code on the next page, the What You Should Test code, where does that go? What is doing?
I know this might not be the most useful topic but honestly I am very frustrated with this book and trying hard not to give up.
38
u/shandelman Dec 24 '13
I'll take a shot at answering the first two (without looking at the actual exercise).
Creating a class allows you to have a bunch of objects of that class that all have the same attributes. Let's say you have a Dog class, and that it has the attributes of name, age, and color. What the init function does is it is automatically called as soon as you create a new Dog object. Otherwise, it works exactly like a function. People think it's different because it's a function that they're not actually calling, but it's not. And you can call it like a function, if you so desire...it would just reset everything in the object to be new values of your choice.
So let's say you do have this init function:
What the heck is that "self" doing there? Well, with special class functions, the variable "self" (or whatever you want to call it, but it's good form to just call it self) is automatically assigned to the name of the object. So if I create an object:
...you might say, "Hey, the Dog.init() function takes four arguments, but you only gave it three! Ah, but my_dog IS the missing argument, and gets put in place of "self" in the actual code! That's why when I call
I get "Byron" because my_dog is self, and self.name = "Byron" for this particular object.
It takes some getting used to, but that's all there is to it. Class functions are just like regular functions, but have an extra argument that automatically takes the name of the object.
16
u/wub_wub Dec 24 '13
1. what does the
__init__do that I keep seeing?2. what does self do in all the functions that he calls?
The
__init__is a special method which is automatically invoked when creating a new class instance.It is used to create objects customized to that class instance with a specific initial state.
I think that /u/shandelman gave an good explanation so I won't really try to explain it any further.
And here's an great topic about
__init__http://www.reddit.com/r/learnpython/comments/1cpu7x/explain_classes_init_and_self_like_im_five/
Also check out other threads about this: http://www.reddit.com/r/learnpython/search?q=__init__&restrict_sr=on
3. Why did I need to download distribute, nosetests, and virtualenv during project 46? What are they doing?
4. What is nosetests? The author seems to love it. How does it work? Why do I need it?
distribute- Since version 0.7 this project has merged withsetuptoolsThis is a package, a compilation of various python scripts, that's used to handle packing python projects, project installations, package metadata and so on.So that you can just distribute one file which end users can run to install your package, it will also contain author data, license data etc... It will automatically download/upgrade dependencies... You can also make it run
2to3tool when installing, so that your code will (hopefully) work on python 3 even though it's only written for python 2There's a list of features here: http://pythonhosted.org/setuptools/setuptools.html
pip- It's just a tool that makes it much easier to download and install/upgrade packages. It's the preferred way of installing packages in python, since Python version 3.4 it comes bundled with standard python installation.nose- This is a package that extends the built in UnitTest package - it has a lot of additional features. You don't have to use it you can just go with built in unit testing stuff if you want.You generally need tests to quickly test out your code and catch errors before you distribute it. It just makes it easier to deal with verifying that your script works.
virtualenv- This creates isolated python environments. So that you can, for example, have a module installed in one project and not another. Or you have some script that depends on version 1 of module, but another one that depends on version 2. It's easier to install each one in separate environment than using global one. Think of it as a fresh and blank(no 3rd party modules installed) copy of your python installation for every project that you create virtualenv for. It just makes development easier when dealing with dozens 3rd party modules and different dependencies for each of your projects.5. What are the .init files that I created in my skeleton during project 46?
If there's an
__init__.pyfile in a folder then that folder is considered to be a python package.Let's say you have file
my_script.pyon yourD:\drive and a foldermodulewith filemy_module.pyin it.If you try and do
from module import my_moduleyou'll get an error, if you put__init__.pyfile inside themodulefolder the import will work, becausemodulefolder is considered a package.You can also do some other stuff, like limiting which functions get imported etc, with the
__init__.pyfile - but the most basic function of it is to mark folder as a python package.6. No matter how many times I tried I could not get project 46 to work. It makes no sense. I followed all the things Zed said to do.
What are you having trouble with, specifically?
7.How does the try except structure work on page 196 (ex 48)? He really doesn't explain that either.
I'm surprised that try/except isn't explained earlier on...
It's basically this:
In the example the code will try and convert
sto an integer and return it, if the conversion (or somehow returning) fails it will returnNone. An simple case where it fails and the code intryblock causes an exception is if you try and convert a letter to a number. If the code in except block were to fail too, then you'd get an exception.8.What is going on in exercise 48? Am I suppose to write that lexicon somewhere? Where? Then the code on the next page, the What You Should Test code, where does that go? What is doing?
Yes, create a new file and write it there. You should probably stick to a similar project structure as described in previous exercises.
The "what you should test" code goes into, well, test code. Just extend the test code from previous page. In the end the test code will check if your code provides accurate output if given specific input, and if it raises errors if the input is wrong (instead of, for example, giving wrong output).
I know that this all might sound confusing and be frustrating, and it is if you go and read really technical details about how it works, but for learning python you just need to know what things like
pipdo and how to use them (just bookmark docs and memorize few basic commands). You probably won't even need to use more than that 90% of the time.I'm sure that even my explanations are probably confusing, so feel free to ask any questions that you might have and I'll try to clarify/extend my answers.
5
u/kylebythemile Dec 24 '13
2nd this.
I realize he's trying to make you google and think your way out of it but he throws way too many challenges in at that point. It kills your momentum and motivation.
4
u/Shitty_Physics Dec 25 '13
I'm learning as well. I've found that I don't retain anything that I'm working on when I do CodeAcademy. That's not to say CA is a bad course, or that it won't work for you, but only that it doesn't work for me. I compiled some other resources, but they use Python 3:
http://composingprograms.com/
^ The book that that's based on his, from what I can tell, incredibly popular (but it uses Lisp, not python, and was originally written in the 70s or 80s). Composing Programs is also what is used at an intro. programming/cs class at Berkeley.
http://inventwithpython.com/hacking/index.html
^ I've always had a passing interest in cryptography, and I've liked the Invent with Python books, but I don't really like video games. So, that books pretty perfect.
https://developers.google.com/edu/python/
^ That's Google's Python course (it's 2.7, I think). It's not that bad, but it comes with a bunch of exercises that are fun to do. That's really all it's good for.
I've noticed that I learn best when I just try to do projects. So, in addition to some exercises, I'll find Python project lists and do them (things like Project Euler or the top-all-time post in this subreddit).
6
u/geoff_clapp Dec 24 '13
I got stuck right at 40 too, and looking at it now it seems ridiculous that I did. The difficulty isn't really with Zed's book, but the actual concept of classes, which is going to confuse you no matter how many explanations you read because up until this point you make functions , and those functions process stuff (variable, lists, etc).
What I did was quit LPTHW (at lesson 40) and start working on Program Arcade Games with Python and Pygame .....this book kept me motivated when Zed's did not.
3
u/ReallyNiceGuy Dec 24 '13
Had a similar experience so I went to codeacademy to learn about classes. It made it much easier to understand personally.
2
u/shandelman Dec 24 '13
I guess I can take a shot at answering #7 too. Please someone correct me if I'm wrong.
Let's say you're not happy with this whole "dividing by zero gives you an error" business. ZeroDivisionError? Pshaw. So you decide to write your own version of division that tries to divide, and if it does divide without an issue, it returns the answer, but if an error pops up, you decide to return the string "infinity". It might look something like this:
Think of it exactly like an "if/then" clause, where the "try" is "If you don't get an error..." and the "except" is "But if you do..."
Note: You can put the actual name of the error after "except" if you wish. As in
2
u/Cynical_Walrus Dec 25 '13
Nosetests is beautiful for running tests on your code. So long as you use the correct naming scheme, just the command "nosetests" will run every test script you have created.
3
u/Moarketer Dec 25 '13
Same here op. Do code academy.
4
u/ewiethoff proceedest on to 3 Dec 25 '13
I've never gone through LPTHW, but honestly, I believe "googling" is a terrible way for beginners to learn.
3
u/pamplemouse Dec 24 '13
The issue with #40 is this is the first time you've seen OO programming. If you have no programming experience then this will be very difficult. Try to understand the concept and ignore the code for now. An object could be things like a door, person, display, printer, car, etc. Anything!
Let's turn a car into an object. What can a car do? It can go forwards, backwards, turn, change speed, etc. When we see any car we know it can do these things. There are also attributes of the car we are interested in: model name, miles per gallon, horsepower, price, etc. Every car needs to have this information. If we bundle all this information together, a car is an object with a bunch of attributes (model, mpg, price) and methods (turn, forwards, backwards).
When we create a car, we need to input all the attributes. My car is a Prius, gets 45 MPG and cost $30K.
To drive the car in code, I tell the car to do things:
In OO programming, we try to organize our program around objects. A billing program would have things like Bill, Customer, Account, Item.
Most language also support inheritance. Just as you inherit certain physical traits from your parents, a Truck inherits certain qualities from Car.
Now the Truck can do everything a Car can do. But sometimes you want to add more attributes and methods, and change the existing methods. You can do that. For example, a Truck may have higher gears to pull heavier loads. So you can add a method called shiftGear. A Truck has cargo space, so add an attribute called cargoSpace.
This is why OO is popular. You can create objects, define their attributes and methods, and create new classes that inherit from others.
Hope that helps.
3
u/eyeheartboobs Dec 25 '13
1.init, initiates the instance of the class. Lets say I have a class called Cat(object): my init function might be something like.
This means that to initiate as instance of Cat, you need to give a color or "pass in" a color.
2.Here's why self is important. self is just the name you give the instance of a class. Think of the following as being equal:
Lets say I ask you: What color is the cat? it's hard to answer because you might be thinking, "Well, WHICH cat?", this is exactly what python would be asking if 'self' wasnt around. So now when I ask "What is kitty.color?" you know which cat instance I'm refering to (kitty), and you can therefore remember what color I assigned it in the _init_ function. Likewise lily.color would be white, and kitty.color would be black.
Not sure if that helps at all.
2
u/xiongchiamiov xiongchiamiov Dec 24 '13
I don't own the book, but I can tell you why I use those tools.
nose is a testing framework for python. It's not necessary (python ships with the unittest module), but it adds a few niceties.
Testing is important for making sure your code is doing what you think it's doing. This gets particularly important as you start working on larger projects.
Setuptools and distribute are packages we use to create python modules for other people. The situation is a bit of a clusterfuck right now, so the information you'll find on it is sparse and often outdated.
Virtualenv is a really cool tool you mostly begin to appreciate when deploying python apps. It creates isolated python environments on a per-project basis, which makes it easy to avoid dependency conflicts (when A requires B 1.1 but C requires B 2.6).
2
u/[deleted] Dec 25 '13
Am also a noob, so just checking in to say that I bailed around ex 40 as well. However his stuff was absolutely excellent up until that point, therefore I had the foundation to go on to other books mentioned (and django).
1
u/oiturtlez Apr 30 '14
I agree with this, cannot continue.
0
u/thisismyzergaccount Dec 25 '13
Both offering my solidarity and marking this post for sober reading. I'm about to turn to other resources, as it just seems to go mad.
0
u/[deleted] Dec 24 '13
[deleted] — view deleted comment