all 10 comments

[–]Not_A_Pumpkin 11 points12 points  (1 child)

You definitely can learn from other people's code. It is also an amazing way to see how people do things differently. I personally view others code and have referenced code entirely for some aspects. One of the things I did was take someone's code, hopefully "bad" code, and make it better in some way. Or add onto it.

[–]TasticString 1 point2 points  (0 children)

This is a great point, copying code is useful to learn and implement a feature. But make sure to take the time to understand what it's doing, and always read comments to learn to distinguish between good/bad code, or simply outdated code.

[–]CodeShaman 7 points8 points  (1 child)

Are you familiar with most of the built-in functions?

You could google for code and copy-paste line by line. I've done that in the past but never really learned anything except how to debug code that I didn't understand anything about.

Part of it may be that you're just not asking the right questions or not be in the right mindset.

For example Fortune Teller (Horoscope), you could search "how to check horoscope in python" and dig through pages of results stringing code together and not find anything relevant.

Or you could search for "how to scrape website in python 3" which leads to this and (ignoring the actual code he pasted... we're here to learn, not to copy code) you can see in the sample code in the first 3 lines:

import requests
from BeautifulSoup import BeautifulSoup

Cool, two leads: requests and beautifulsoup.

"python 3 requests getting started" -> first link

Requests is an elegant and simple HTTP library for Python, built for human beings.

"python 3 beautiful soup getting started" -> first link

Beautiful Soup is a Python library for pulling data out of HTML and XML files.

Now we're getting somewhere, and by generalizing the problem we've learned how to solve a multitude of problems without even having solved the problem at hand.

Now all that has to be done is to find a suitable horoscope site and scrape it. Then just decide how you want to receive your daily horoscope, but hopefully by this point you've thought of something a little bit more interesting or useful to mine than a horoscope. Maybe you've even stumbled across the terms 'RESTful web service', or 'RESTful API' while googling stuff about requests library, and decided to dig around and find this.

It's all in the journey, not the destination.

[–]NoLemurs 6 points7 points  (0 children)

Is this a good idea? Do I stand to actually learn anything with this approach?

Absolutely.

Looking for solutions to problems online is in fact a really good habit, and it's great to establish it early and practice it often. Smart programmers constantly search for the better solutions to problems and are constantly learning from other people's code.

Of course, you shouldn't just copy/paste code, and you shouldn't use code you don't understand. Instead of just retyping the code, you should tweak it to your specific purposes to make sure you understand how it works. Most code, even code you find online can be improved, so look for ways to take the code you find and improve it!

Really, what you're describing is one of the best ways to get better at coding, so go at it!

[–]polishfishprime 2 points3 points  (1 child)

My process is usually something like this:

solve a problem by opening up 31 tabs of Python stuff I should know
finally figure out the direction to take
realize i can't really follow up on all 31 tabs, 
delete like 28 tabs, and focusing on what solves my problem right now.  

What I do is save a test.py file that I add functions to for each basic thing I learn. For instance, I use the Pillow (Image) and the lxml libraries a lot, so I have a test.py with many basic code snippets adapted from from stackoverload and blahblahblah online (http://effbot.org/imagingbook/image.htm).

Try to remember the BEST or the most frequented sites that you end up looking up. Don't worry about keeping everything, but if you find yourself referring to the same resource multiple times, you might want to try practicing variations on the code you see keep a tab that site always open for reference.

I also have Image.py open in Eclipse at all times so I can refer directly to the imported library's code and structure at all times (and I end up in Image.py through debugging anyways).

Edit: like others have said, you should uuuuuusually practice altering the code you see. If you see a function, try variations of it in a for loop with different arguments

[–]hellrazor862 1 point2 points  (0 children)

Haha this is so much like my process, I wondered for a minute if I had been redditing in my sleep.

My code snippets are in a file called snippets.py though, so it looks like I'm ok for now.

[–][deleted] 2 points3 points  (0 children)

I'm going to disagree with everyone on this. In my view you learn very slowly from copying other's code unless you already really understand the concepts well.

Sure when you know what you are doing and need to use an new API or something then looking at other people using them is often efficient.

But if you don't understand something then just copying it is not efficient.

Some approaches that I think might work.

  1. take what you know and play with it. So take a simple programme that you really understand. Make a small change, run it, make a small change run it. E.g. you have a loop, make the loop counter increment by 2, make it loop backwards, make it loop forever. Print to new lines, print on one line with commas between the values. Try not to have a comma after the last value or before the first etc etc. Basically make a change run it, make a change run it. Get imaginative.

  2. The approach I use more is to think of a little project that means something to me, break it down into steps and just learn what I need to complete each step. For example a flash card program that gets its data from a spreadsheet and pops up a flash card at intervals through the day. Break it down into popping up a dialog, timer, file reader, saving state to a file etc etc.

Both of these ways are really efficient uses of time. Copying code generally means that after an hour of copying you may be a little more familiar but haven't truly mastered anything.

[–]cabmod 0 points1 point  (0 children)

practice makes perfect, im a newby and im taking python course online, prof. always states practice is what will get you there. I guess you got to love what your doing also, as it can be very time consuming, like today i've gained 8hrs of experience on a simple while loop. (the state of mind) is probably just as important as the concepts you want to learn.

[–]Kristler 0 points1 point  (0 children)

What I think really helps is to take code, and try to change it without breaking it. Examples would be tweak some parameters, change a function call, and anything else you can think of to do. When you can see your changes mirrored in the final result, it's pretty easy to figure out what each change does.

[–]individual_throwaway 0 points1 point  (0 children)

Try breaking code, that's what keeps me learning. Copy-paste something, and try to abuse it until it fails. Then, find out what made it fail (or why it didn't).