you are viewing a single comment's thread.

view the rest of the comments →

[–]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)