Resource mentioned in this lesson: Python Class Constructors: Control Your Object Instantiation
Working With Attributes
00:00 With object-oriented programming in Python, objects can have two kinds of attributes: class attributes, which are defined directly in the class body and shared by all instances of that class.
00:11
And instance attributes, which are typically assigned when an instance is initialized and are unique to each instance. So you’re probably wondering, what is initialization? __init__() is a special method that runs when a new object is being created, and dunder is a weird word, but it’s a contraction of double underscore.
00:32 Python provides many of these dunder methods and attributes, and you can use them in your classes to customize their behavior in myriad ways. Because they’re so useful and also a little odd looking, their other names include special methods and magic methods.
00:47
So here’s a version of the Crew class with a few attributes. class Crew: species = the string "Human" def __init__ with the parameters (self, name, age): self.name = name and self. age = age. The __init__() method is called when an object instance is created and it can have any number of parameters.
01:08
However, the first parameter should always be self. This parameter will correspond to the newly created object, which is always passed to the __init__() method.
01:17
Within the __init__() method, you can write whatever logic you want, including assigning instance attributes or running any custom logic you may want to include when objects are instantiated, like validation, for instance. Here, species is a class attribute and available on all instances.
01:32
It’s defined inside the class, but outside of any methods. Within __init__(), self takes on the value of the newly created object and then name and age are assigned to self as instance attributes.
01:46 Head to the REPL again, and we’ll give it a try.
01:49
Let’s flesh out the Starship class. class Starship: ship_type = the string "Exploration Vessel" def __init__(self, name, crew_size):
02:06
self.name = name, self.crew_size = crew_size.
02:12
Now you’re set up for three attributes: the class attribute, ship_type, and the instance attributes name and crew_size, all relevant properties of a Starship.
02:21
The first difference you’ll notice is that now creating Starship instances has changed. Call Starship() and you’ll get an error.
02:29
Starship.__init__() is missing two required positional arguments: 'name' and 'crew_size'. __init__() is still a function and it needs to be called with the right number of arguments.
02:39
These arguments are passed when you call the class to create an instance. And remember, self is passed automatically. This is why the error says that only name and crew_size are missing.
02:49
Now you can create a couple Starships.
02:52
enterprise = Starship("Enterprise") and the integer 430. serenity = Starship passing in the string ("Serenity") and the integer 9.
03:04
You can grab their names with the .name attribute via the dot accessor: enterprise.name is "Enterprise" and serenity.name is "Serenity".
03:14
Get their crew sizes in the same fashion. enterprise .crew_size is 430,
03:22
serenity.crew_size is 9, and even though you didn’t specify it, they both share the .ship_type class attribute. enterprise.ship_type, "Exploration Vessel", serenity.ship_type, also "Exploration Vessel".
03:37 Because of this class-based approach, you know all instances will have the same interface. You can treat each of them the same way. Something more particular to Python is the fact that, by default, the classes you make are also dynamic and mutable.
03:51
You can modify their attributes directly on the fly. You can, say, add a crew member to serenity.
03:58
Set serenity.crew_size = 10 and see that it has changed.
04:03
serenity.crew_size is 10. What’s more, you can modify an instance’s value of a class attribute, like setting serenity.ship_type = "Cargo Ship" and see if it’s updated.
04:17
serenity.ship_type is now "Cargo Ship". However, this does not modify other instances. enterprise.ship_type remains "Exploration Vessel".
04:29
We’ll continue working with the __init__() method over the next few lessons, but if you’re really curious about how objects are created in Python, check out this course: Python Class Constructors: Control Your Object Instantiation.
04:43 Now that you’ve given your class properties in the form of attributes, next up, you’ll provide behavior in the form of methods.
Become a Member to join the conversation.
