TeachingDataScience / data-science-course

Data Science Course Materials
11 stars 16 forks source link

Init! What is it? What is it used for? #1

Closed datadave closed 9 years ago

adrianbautista commented 10 years ago

def __init__(self): is the initializer method used to create instances (objects) of a Python class. It's in this constructor method that default values or arguments get set as attributes on the instance of the class (the self object).

class Student(object):
  def __init__(self, name):
    self.name = name # sets the name attribute on a Student object when created

data_student = Student('Tico')
print(data_student.name) # prints "Tico"