Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Objects __init__

self is not defined?

I make a class with the name attribute. The second step is to make a init method but it will say task one is no longer passing. I go to task one and run it again. but then it says name 'self' is not defined

student.py
class Student:
  name = 'Phil'

  def __init__(n):
    self.name = n

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hola,

You must remember that a function is like a black box. Performs some work by it'self. Unless you are doing functional python but we are not.

A CLASS contains methods. When a function is added to a class, it's first argument is always the "self" keyword. This identifies the function as a method because it's part of a class. You can call functions outside your class too, but the take home is that a function stands alone and a method is part of a class. Because it's part of a class it always has the "self" keyword for the first argument.These words are used interchangeably so it can be confusing.

Ok, so you add the "self" keyword as the init methods first argument. Ok moving on to part 2.

Now we are setting this variable in the context of this class. So I use self.name. Which it looks like you did. What about these default keyword argument you ask?

Well, in java we can override constructors and use more than one. In python, it's not really like that. In python, the init method will prepare the class with what it needs when it's created just like a constructor in java. Except, what if I don't pass in all the values required? For example. Most cars have 4 wheels. So if I am creating a car class. I might want to pass into my init method, the color, brand, and how many tires it has. Well I know 99 percent of cars have 4 tires. So I don't need to make the user pass that data unless it's something other than 4, which I will call the standard. This is where default args come in.

With a default value, if I create the class without passing it 4 tires. Say I just pass it color and brand. If I say (tires = 4) in the agruments. Like soo

def init(self, color, brand, tires = 4):

It will set tires to 4. when I pass in color and brand. UNLESS I pass in a different value to tires. If I pass in say 3 for tires, the default value is not longer used and the value I passed in is used. So default value is 4 if no other value is passed in. Since python argument order matters. tires will always be the last thing I pass in so Python knows it's tires since it's the last argument in the method.

Now, being we are adding a default argument. We must give it a value in the init method. Like I did in the code below.

So to answer your question. Self is not defined because the init method doesn't start with self. And your n value also has no value. You didn't give it a default value. So as far as python knows. self.name is blank and or undefined.

Override init in Student. Give init a name keyword argument with a default value of your choice. Set self.name equal to the name argument.

class Student:     #exercise 1
  name = 'Phil'

#exercise 2
  def __init__(self, name="Ryan"):   
    self.name = name                          

Let me know if this helps you and doesn't confuse you more.

Thanks!