Explanation regarding the self as argument


#1



https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/3/2?curriculum_id=4f89dab3d788890003000096#

Error Message Includes :
Oops, try again. In your check_angles method, make sure to check the sum of self.angle1 + self.angle2 + self.angle3.


Hello! :slight_smile:
I would like an explanation regarding self as argument in my code, I thought this code would work. As per the hint, I am supposed to replace the Triangle argument in def check_angles() method by self as an argument.
I would really love an explanation that would be as simple as possible..

Also, Please can anybody explain inheritance? I still feel I haven't understood the concept well enough!

Thank you in advance!
Cheers!


"""Creating a Parent Class called Triangle and initializing the class using __init__()"""

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    #Initializing the member variable inside Triangle Class.
    number_of_sides = 3
    
    def check_angles(Triangle):
        if self.angle1+self.angle2+self.angle3 == 180 :
            return True
        else :
            return False


#2

You're using the name self but you haven't created a variable by that name, so you'll obviously get an error saying self is not defined, so I'm not sure what the confusion is about?

As for general information on inheritance, that's been done, there are hundreds of explanations a search query away!

Also, don't use an if-statement to return True/False when you already have an expression that evaluates to that same value. Just return the result of the expression.


#3

"The first argument passed to init() must always be the keyword self - this is how the object keeps track of itself internally - but we can pass additional variables after that." ( Codecademy )


#4

self is not a keyword, that name has no significance in the language other than by (strong) convention


#5

hmm, 'parameter' then, is more appropriate?


#6

I'd rephrase that to something along the lines of:
Methods called on an object receive a reference to said object as their first argument, the first parameter of methods in Python are therefore named self by convention.


#7

Yes it is a convention, I just read about it on StackOverflow


#8

Thank you for your reply! :slight_smile:
I totally appreciate it! I am new to programming and still learning bit by bit


#9

What I meant is, when i put Triangle as an argument for def check_angles() function, the code did not work. But when I replaced that by self. It worked. I didn't know why it worked.
And Also I am still confused about self. I would read into it more later..
I am absolutely a rookie in programming, Still learning bit by bit.

I thought someone could explain it using a very simple example.

I really appreciate your reply :smiley: sir!
thanks!


#10

You were not giving it arguments, you were giving it parameters. Arguments are the values sent, parameters are what the method accepts.

Your method uses the name self, so if you change the place where you create that variable (parameter), then you'll be trying to access something you have not defined, it will crash.

Changing the name of a parameter doesn't break anything, but using something that you have not defined, does. To get access to the first argument, you will need to refer to the first parameter.

The first argument sent to a bound (associated with an object) method when it is called, is the object itself. A method defines some piece of behaviour for an object, and in that context the object will be called something like self, just as when you move your arm, you are self


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.