Needing tips for classes and objects

Hello,

So I started learning Python 3 every day and I started officially on June 1st 2020. I’ve been spending a few hours everyday coding on Python 3 from the very start which is the Syntax and have completed every step of the way including projects and quizzes that are in the pro membership, I’ve now reached classes and objects and I’m starting to understand the constructor, self etc which I had been taught in class but always found it hard since I didn’t have the basic fundamentals to start with, but now I do kind of so far. I’ve only done the projects that were in the Python 3 course and not the ones listed.

So for this exercise in Learn Python: Classes, I do have a basic understanding but as we move on to the Learn Python: Inheritance and Polymorphism part, it starts to feel a bit confusing and overwhelming as I’m given all these things to learn. Here you can see what I mean by feeling a bit confused with all of these methods.

image

By providing the information above, I would like to ask, what is the best way to learn it, I have tried and watched Youtube video’s explaining what it does which I’ve found useful etc.

But for example, this piece of code which is the 7th Dunder Methods exercise, I found confusing for the add method especially the part for the new_red = min(self.red + other.red, 255). I don’t understand what it does or means.

def add(self, other):
    """
    Adds two RGB colors together
    Maximum value is 255
    """
    new_red = min(self.red + other.red, 255)
    new_blue = min(self.blue + other.blue, 255)
    new_green = min(self.green + other.green, 255)

    return Color(new_red, new_blue, new_green)

I also have another question regarding Googling everything. Do I only google things I don’t know how to do or google everything even If know how to do it but forget like a for loop, not just the syntax but even the concept? I know the point of coding is to learn how the concept works, but when I learn so much code, I tend to forget how things work, obviously, I know how a for loop works but it’s just an example.

Thanks

1 Like

I find having a handy reference book can go a long way (I use O’Reilley’s Python book among others). Corey Schaffer’s youtube channel has also gone a long way for me when I was getting started on Python.

Concepts that are new require not only practice, but reflection. At the end of your practice session, try to list a number of questions you have about the nature of what you just studied. Often, those questions will be pointers to better understanding the subject. And as a bonus, these questions will usually be a focused way to google than a general sweep on the subject.

2 Likes

It does exactly as it says in the comment. :slight_smile:
Adds two RGB colors together
Maximum value is 255

Which part of it don’t you understand? Is it the min, or the self.red and other.red?

Also, don’t worry about constantly googling things. You will always be forgetting the things you learn unless you practice them a lot or use them regularly.

2 Likes

Hello, @object4537861680, and welcome to the Codecademy Forums!

Let’s first look at how the add method of Color is used. The example code includes the following statements:

red = Color(255, 0, 0)
blue = Color(0, 255, 0)
magenta = red.add(blue)

magenta is the result of adding red and blue together. When the add method is called in the third of the three lines of code listed above, self represents red and other represents blue.

Each of the three components of the resulting magenta instance of Color should be the sum of the corresponding components of the red and the blue instances of Color that are being combined. If, instead, magenta had been created directly via the Color constructor, it would be been done as follows:

magenta = Color(255, 255, 0)

In combing two Color instances, we need to guarantee that the value for each of the components will never exceed 255. That is the purpose of using the min function with 255 as the second argument. If the sum of any of the components exceeds 255, the min function will return 255 as the result. That would be relevant, if we did something like this:

yellow = Color(255, 255, 0)
cyan = Color(0, 255, 255)
very_light_color = yellow.add(cyan)

The two green components would add up to 510, but green would be limited to 255 in the result. In fact, all the components will have a value of 255 in the result, and very_light_color would turn out to be white.

Edit (June 30, 2020):

The following line, cited above, is copied from the exercise page:

blue = Color(0, 255, 0)

However, the color components really represent green rather than blue.
Thanks to @mtf for noticing that.

2 Likes

Alright, thanks. Is it normal to not understand some of the lessons? And do you have tips for me to continue to learn and get better? Like I said I do code every day for a few hours but most of it is me learning what the code does as to suppose writing code. Like before I used to code all the time when I started but now since I’ve reached a complicated stage, I’ve been watching a few videos and reading other sources to understand the concepts if Codecademy doesn’t teach me it properly.

Should I be practising after learning each lesson? I feel like I should be, but since I started all I’ve done is just follow the Codecademy route and completing them, understanding them and getting them correct and doing there recommended projects but I’ve not yet done the external projects that they recommend.

As for googling everything I feel like if I google things I already know I just take some hints and put them in and try and see if it works, should I be doing that?

Yes, it takes time and practice to learn this type of material.

If you find that it helps, as would be the case with most people, then by all means, do it, and have fun with it.

Sometimes it takes effort to convince users that they should utilize web searches to help themselves learn. If you are already googling things, that’s terrific - you are on the right path.

2 Likes

Isn’t that green?

blue = Color(0, 0, 255)
3 Likes

Yes, oops!

:blush: <-- and that’s red :wink:

Edit (June 30, 2020):

Some of the examples on the exercise page have it wrong, for example …

red = Color(255, 0, 0)
blue = Color(0, 255, 0)
green = Color(0, 0, 255)

… and I hadn’t noticed.

3 Likes
  def add(self, other):
    """
    Adds two RGB colors together
    Maximum value is 255
    """
    new_red = min(self.red + other.red, 255)
    new_blue = min(self.blue + other.blue, 255)
    new_green = min(self.green + other.green, 255)

    return Color(new_red, new_blue, new_green)

I’ve been going back and forth, but I think I now understand what is happening after watching one of Corey Schafer video’s about the magic/dunder method. So correct me if I’m wrong.

But what happens here is that we assign the new_red to this: new_red = min(self.red + other.red, 255) which get’s the smallest value, out of the two inputs placed in the parenthesis. After it adds both the self.red and other.red and if the calculation of both those inputs and compares it to 255, (the second input) and which ever is the smallest then get’s applied to that variable, which in this case it’s new_red.

I hope I’m correct in describing what it was doing, as that is what I’m pretty sure the min() function is meant to do. Please let me know if I did it incorrectly

Thanks

2 Likes

I may be wrong but it seems you’re confusing the order a little (perhaps not your logic, just the way it is worded). The expressions on the right hand side of the assignment operator are evaluted first and then assigned to new_red.

Otherwise you seem to have the correct interpretation arguments passed to a function are evaluated before the funciton executes and min() would indeed return what it believes to be the lowest value in the sequence passed to it.

2 Likes

@tgrtim Yes!

I would also add that contextually this function is within a Class (because of the reference to self).

So:

  • the function exists in a class that has an attribute of self.red, self.blue, self.green.
  • this function takes an input that itself has attributes of self.red, self.blue, self.green, but in here we see them as other.red etc.
  • some function min() performs an operation on these (for more context on this, you’d have to look into how colors work, which is an interesting tangent)
  • a new Color() is returned (notice this is a class object as well, with the aforementioned attributes)