Classes and the __init__ Method
Define classes, create objects, and initialize attributes.
Defining a Class
Use the class keyword to define a class. By convention, class names use CapWords.
class Dog:
pass
fido = Dog()
print(type(fido)) # <class '__main__.Dog'>The __init__ Method
__init__ is called automatically when an object is created. Use it to set up instance attributes.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
fido = Dog("Fido", "Labrador")
print(fido.name) # FidoAll lessons in this course
- Classes and the __init__ Method
- Instance vs Class Attributes
- Instance Methods and self
- Magic Methods: __str__ and __repr__