Course 1.1Lesson 2 of 5
A class is written with the word class, a name that starts with a capital letter, and a block of methods. The method __init__ runs when an object is made and sets up its attributes, and the first parameter of every method is self, the object itself. Attributes are made by writing self dot name equals value, and they belong to one object. You make an object by calling the class like a function, and each object has its own data. A method without self, or an assignment without self dot, are the two usual mistakes.
Try the code and the simulations beside the lesson.
After this lesson you can
__init__ and methods,self is, andself.Imagine that you run a pet shop, and you keep a record of every dog: its name and its age. You also want each dog to be able to bark, and to have a birthday. You could keep a dictionary for each dog. A class does it more neatly.
Predict first: what will the last lines show? Then run it.
Now the objects.
rex = Dog("Rex", 3) makes an object. You call the class like a function. Python makes a new empty object, runs __init__ with that object as self, and gives the object back. We say that rex is an instance of Dog.rex.name and rex.age read the attributes. rex.bark() calls the method.rex.birthday(), rex.age is 4, but bella.age is still 5. Each object has its own data. The class is shared. The data is not.The word self confuses everybody at first. Here is the whole story. When you write rex.bark(), Python does Dog.bark(rex): it calls the method of the class, and passes the object rex as the first argument. Inside the method, that first parameter is called self. So self is simply the object that the method was called on.
You can call the first parameter anything. But always call it self. Every Python programmer expects it.
self is the object that the method was called on. Inside area(), self.width is the width of that one rectangle.
Mistake 1: leaving out self in the method.
Mistake 2: forgetting self. in front of an attribute.
A rule to keep: an attribute is always written with self. in front, inside the class. A bare name inside a method is a local variable.
self in the def line. A TypeError about arguments.self. before an attribute. An AttributeError.Dog without brackets. rex = Dog does not make a dog. It gives another name to the class.__init__. Dog() without a name is a TypeError.__init__ with one underscore, or the wrong spelling. Python does not run it.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
class Name: and methods inside. __init__(self, ...) sets up each new object.self.attribute = value makes data that belongs to one object.Name(arguments) makes an object. Each object has its own data.self is the object that the method was called on, and it is always the first parameter.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Dog("Rex", 3)?bark is written def bark():?name = name inside __init__?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.