A question about __init__()


#1

I was working on the “Class Basics” section and I had my code right but codecademy wouldn’t let me proceed. I originally had my variables set up as angle1=self.angle1 in my init() method.

However, when I compared my answer to the website, they had self.angle1=angle1. This was the only difference. My question is does the order matter? does self.(whatever it may be) ALWAYS come before the “=” sign?


#2

angle1 is the parameter. self.angle1 is not defined.

self.angle1 = angle1

is how these should be written.

Yes, since we are only just then defining the instance variable. self is the context object for all intensive purposes. It is the container for the current instance.

my_person = Person('Roy', True)

The above is an instance of the Person class, which might look something like this…

class Person(object):
    def __init__(name, is_married):
        self.name = name
        self.is_married = is_married
    def __repr__(self):
        return "This is {}, and he/she is {}married.".format(self.name, '' if self.is_married else 'not ')

my_person = Person('Roy', True)
print my_person

# -> This is Roy, and he/she is married.

#3

Thank you mtf. I also appreciate being shown another approach on how to to test values in your line containing the last “return” you used there.


#4

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