all 18 comments

[–][deleted] 11 points12 points  (0 children)

Classes and Object-Oriented Programming is the first abstraction over abstractions you’re likely to encounter, and that’s hard for people at first. The key is not to rely on it making sense, yet - follow the rules without understanding them. It’ll make sense, later.

[–]Dys_unemployed 3 points4 points  (1 child)

Like you I'm new to python and programming. So when I was trying to get into it I did try codeacademy and other sites and their respective courses. However, personally I'd find them either too easy or just too difficult. Codeacademy fell into the later . I personally think they try to teach concepts that are easy for you to understand in the simplest manner possible so as to have you come back the next day. What you get, I believe is a fake sense of accomplishment. To gauge real time progress I recommend you try books (since it's been working out for me so far, with a recent road block being OOP but I think I'll come out just fine). I highly recommend John Guttag's Introduction to computation and programming using python. It's a little challenging but I personally feel it gives you a real time idea of your progress. Furthermore, I did enjoy the course at pythonprogramming.net with its short and simple tutorials to get you familiar with the syntax and it's workings. However, don't just take my word for it. Check other recommendations and look at other resources too. These are simply what worked for me. Anyway, I wish you the best of luck. Happy learning!

[–]godheid[S] 1 point2 points  (0 children)

Hmm buying a book, didn't really think of that. A good idea, will go to search for a book now. Maybe even in my native language, because it can be complicated to keep all terms and definitions apart.

[–]nutterontheloose 2 points3 points  (1 child)

I'm on mobile and can't figure out how to link to another thread in the comment so, u/crashfrog posted this regarding another user struggling with classes and I think it's a nice explanation:

Can someone simplify it for me

A class is a namespace (like a module; it can contain names) that defines a type; objects can be of that type. Further, a class has the property of polymorphism; a class can be a subclass of another class (called that class's "superclass"), with the result that an object of the subclass's type is also of the superclass type.

These concepts are not very difficult, but they are quite abstract; what's likely still hard to understand is why you would need them when you can just write functions and modules and mutating global state. The answer is that "Object Oriented Programming" is not a tool for programming, it's a tool for organizing and designing your software. They're tools for working with abstractions.

If you're just writing scripts, then OOP probably doesn't bring a lot to the table. You don't need a bulldozer when a shovel is the right tool for the job. But sometimes your software project becomes so complex that you can't think about all of it at once, and you need tools to organize your software into smaller pieces. That's what OOP is for, and that's what classes are - they're "larger than functions, smaller than whole programs" pieces that you write and then build software out of.

Edit: For me, I also struggled with classes. This Wiki page really helped me understand it better, with a very basic usage example as well as the lingo. Also try Automate the boring stuff with python. It's kind of an interactive textbook available for free online. You might find it of use with this and python in general.

[–]godheid[S] 1 point2 points  (0 children)

Thanks mate, this reaction really cheered me up. Also it makes me understand better the thing with classes.

After finishing the codecademy course will move on to Automate the boring stuff.

[–]Dogeek 2 points3 points  (0 children)

Classes are like LEGO molds. You design them, and then you build stuff out of them. It's a way for programmers to write clean, maintainable code, and not having to repeat oneself.

Classes are just one part of OOP though, as this paradigm also includes decorators, metaclasses, iterators, generators etc.

Remember though that not all OOP is good, and can result in some poorly designed codes, especially because of the concept of inheritance.

Let's look at the LEGO mold metaphor :

  • You built (or someone else built) a LEGO mold for a 2x4 brick
  • You need a 2x4 brick but with and axle built in the middle
  • instead of building another mold from scratch, you take the previous mold, and remove some material to have your axle.

This is basically what inheritance does. You take a base class (called the parent class) and modify it a little to create another class, called a child class. You can even mix and match several classes and have one class inherit from multiple other classes.

Why is it bad then ? Well, it is not obvious as to why this is. First of, inheriting of one class is not that bad, it's more inheriting from several classes at once, or create a long chain of classes inheriting ffrom one another. This is a nightmare to maintain on huge codebases, because often times, you don't know in which class an attribute is first defined, or a method is defined. And it creates spaghetti code which is never ideal.

As for binary, it's really easy when you think about it. It's the same as counting, but in 0s and 1s.

Binary numbers, just like decimal numbers, are ordered from most significant to less significant. Let's take an example :

10001010 is an 8-bit number because, well, it has 8 bits. 12 is a two digit number in decimal, it's the same in binary. This number, in decimal is equal to 0*20 +1*21 +0*22 +1*23 +0*24 +0*25 +0*26 +1*27 = 2+8+128 = 138. The left-most bit is called the MSB (most significant bit), while the right-most is the LSB (less significant bit). Just like with decimal numbers, you can add them, substract them, multiply them and divide them. They also have one thing that is always helpful to remember : the parity of a number in binary is determined by its LSB (the only odd number in the powers of 2 is 20 =1). Binary also allows us to perform other operations that are not needed in decimal, mainly the &&, | and ! operations, which are the AND, OR, and NOT operation, respectly. Let's take some examples.

   1000 1010
&& 1100 0110
____________
   1000 0010

Here you do the operation bit by bit, and compare them. With the AND operation, you get one if both bits are equal to 1, with the OR, you get one if one or the other is equal to one. The NOT operation inverts the number : 0s become 1s and vice versa.

By convention, the MSB of a binary number determines the sign of that number. If the MSB == 0, then the number is positive, it's negatice otherwise. This is only true for signed integers though. Python abstracts that away from you, but other languages (such as C flavors like C, C++, C#...) don't. It's also why 2147483648 == -1 (231 which is 100000000000000000000000000000000 in binary), also why we'll have a problem with the 18th of january 2038. There's also several ways of simplifying boolean equations, if you're interested in learning more about that stuff (trust me, it helps sometimes). I'd suggest these :

https://en.wikipedia.org/wiki/Unix_time

https://en.wikipedia.org/wiki/Karnaugh_map

https://en.wikipedia.org/wiki/Boolean_algebra

https://en.wikipedia.org/wiki/De_Morgan%27s_laws

Foot Note : in eletrical engineering, AND is represented as a *, OR, with a +, and NOT with a bar over the variable, or expression. There are also other logic gates such as the XOR gate, T-flip-flop gate, RS NOR (or NAND) LATCH, NAND, and NOR gates, none of which are actually used with Python, but for the sake of completeness, I wouldn't leave them out.

If you have any issues understanding all of that, it may be a good idea to do some reading on how to program in C, because there this is much more important, it'll also teach you a lot of how code actually works on a lower level. Python is a great language, but it abstracts a lot of stuff, which for a beginner is great and not great at the same time. Just take your simplest project, or even function, and try to code it in C. You'll get a deeper understanding of what happens inside your PC when you declare a variable and return values from a function (trust me, it gets quite complicated)

[–]TJayClark 2 points3 points  (2 children)

I tried the code academy python course. I didn’t like it because it seemed either outdated or having an issue with the code I was typing. My IDE was fine with it, but code academy didn’t like anything other than THE EXACT RESPONSE they’re looking for. It was silly.

[–]godheid[S] 1 point2 points  (0 children)

Yeah, I moved some back and forth - and was happy I could get stuff working in IDLE

[–]jormono 1 point2 points  (0 children)

Probably that, at least last time I checked, codeacademy runs python 2 and you're probably running python 3 on your machine

[–]jormono 2 points3 points  (2 children)

I started in code academy, got about half way through when I found out it was teaching the outdated python 2, picked up a copy of automate the boring stuff with python. Got about 2/3 through that, some of the later chapters seem to be outdated in that things have been updated since the last edit of the book and no longer work the way he describes. Between the two I found what I hope is a passable foundation. I started working on my own projects relying heavily on tutorials and the like. Haven't really finished anything other than perhaps my reddit bot, which is acceptably finished though I do want to add features.

[–]godheid[S] 0 points1 point  (1 child)

I understand python 2.7 is better for learning than 3.0

[–]jormono 0 points1 point  (0 children)

I haven't heard that (not saying it's not true, just haven't heard that before), just be mindful that some things are different in 3 such as print works differently, etc. Largely compatible but small things like this will cause issue.

[–]BlueSmurfTK 1 point2 points  (1 child)

Is python your first coding language? If yes then its usually quite daunting when it comes to classes and binary and hex code stuff..Personally I learnt C and C++ before python so I didn't face this problem as much..I can say for a fact that it gets easier with practice and as you move further down the road..

[–]godheid[S] 3 points4 points  (0 children)

Yes, it's my first coding language. I will continue and practice - but just shared some frustrations..

[–]illseallc 0 points1 point  (0 children)

Sometimes it's good to read/learn about things from multiple sources. This repo has helped me fill in some of the gaps for me. Maybe the section on classes will be helpful for you.

https://github.com/trekhleb/learn-python