__init__ and properties for classes

Question Click to View Answer

What does the following code print to the console?

class Person:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def full_name(self):
        return f"{self.first_name} {self.last_name}"

gaga = Person("lady", "gaga")
print(gaga.full_name())

"lady gaga" is printed.

__init__ is an initialization method to provide objects with data when they're created. When the gaga object is created, it has a first_name and a last_name.

The full_name() method uses first_name and last_name to construct a string.

What does the following code print to the console?

class Mustache:
    def __init__(self, color):
        self.color = color

b = Mustache("brown")

print(b.color)

"brown" is printed.

The Mustache class is instantiated with a color variable. The color variable is accessible once the object is instantiated.

What does the following code print to the console?

class City:
    def __init__(self, country, state):
        self.country = country
        self.state = state

houston = City("USA")

print(houston.country)

This code raises a TypeError with the following message: init() missing 1 required positional argument: 'state'.

The City class must be instantiated with two arguments, so our code that only instantiated it with one argument (City("USA")) is wrong. This would fix the code.

houston = City("USA", "Texas")

What does the following code print to the console?

class Dancer:
    def __init__(self, height, country="Cuba"):
        self.height = height
        self.country = country

salsa_pro = Dancer(183)

print(salsa_pro.country)

"Cuba" is printed.

The country variable is set to "Cuba" by default. If the variable isn't supplied, the default value is used.

What does the following code print to the console?

class Chair:
    def __init__(self, material="wood"):
        self.material = material

my_chair = Chair("plastic")

print(my_chair.material)

"plastic" is printed.

The default variable for the material argument of the __init__ method is "wood". When creating the my_chair object, the default value is overridden, so material is "plastic" for my_chair.

What does the following code print to the console?

class Seatbelt:
    def __init__(self, color):
        self.color = color

my_seatbelt = Seatbelt("black")
my_seatbelt.color = "pink"

print(my_seatbelt.color)

"pink" is printed.

my_seatbelt is instantiated to have a color of "black".

Dot notation is used to update the color property to "pink".

What does the following code print to the console?

class Beach:
    def description():
        return "sandy and wet"

print(Beach.description())

"sandy and wet" is printed.

When methods don't have self as the first argument, they can be called directly on the class. The description() method cannot be called on instances of the Beach class.

What does the following code print to the console?

class Red:
    def feeling():
        return "Lucky!"

r = Red()

print(r.feeling())

This code raises a TypeError with the following message: feeling() takes 0 positional arguments but 1 was given.

The feeling method can be edited to include self as the first argument and the code will run (i.e. def feeling(self):).

Alternatively, the feeling() method can be called directly on the Red class.

class Red:
    def feeling():
        return "Lucky!"

print(Red.feeling())

What does the following code print to the console?

class Horrible:
    def bad_code(weird):
        return "Don't write code like this"

h = Horrible()

print(h.bad_code())

"Don't write code like this" is printed.

This code works, but it's not pretty.

Instead of using self as the first argument to bad_code(), we've decided to use weird. This example illustrates that there is nothing special about the self variable name - Python let's you call this variable whatever you want.

In practice, you should always call this variable self so it's easier for other programmers to read your code.