Learn
Learn Python: Classes
Self

Since we can already use dictionaries to store key-value pairs, using objects for that purpose is not really useful. Instance variables are more powerful when you can guarantee a rigidity to the data the object is holding.

This convenience is most apparent when the constructor creates the instance variables, using the arguments passed in to it. If we were creating a search engine, and we wanted to create classes for each separate entry we could return. We’d do that like this:

class SearchEngineEntry: def __init__(self, url): self.url = url codecademy = SearchEngineEntry("www.codecademy.com") wikipedia = SearchEngineEntry("www.wikipedia.org") print(codecademy.url) # prints "www.codecademy.com" print(wikipedia.url) # prints "www.wikipedia.org"

Since the self keyword refers to the object and not the class being called, we can define a secure method on the SearchEngineEntry class that returns the secure link to an entry.

class SearchEngineEntry: secure_prefix = "https://" def __init__(self, url): self.url = url def secure(self): return "{prefix}{site}".format(prefix=self.secure_prefix, site=self.url) codecademy = SearchEngineEntry("www.codecademy.com") print(codecademy.secure()) # prints "https://www.codecademy.com" print(wikipedia.secure()) # prints "https://www.wikipedia.org"

Above we define our secure() method to take just the one required argument, self. We access both the class variable self.secure_prefix and the instance variable self.url to return a secure URL.

This is the strength of writing object-oriented programs. We can write our classes to structure the data that we need and write methods that will interact with that data in a meaningful way.

Instructions

1.

In script.py you’ll find our familiar friend, the Circle class.

Even though we usually know the diameter beforehand, what we need for most calculations is the radius.

In Circle‘s constructor set the instance variable self.radius to equal half the diameter that gets passed in.

2.

Define three Circles with three different diameters.

  • A medium pizza, medium_pizza, that is 12 inches across.
  • Your teaching table, teaching_table, which is 36 inches across.
  • The Round Room auditorium, round_room, which is 11,460 inches across.
3.

Define a new method circumference for your circle object that takes only one argument, self, and returns the circumference of a circle with the given radius by this formula:

circumference = 2 * pi * radius
4.

Print out the circumferences of medium_pizza, teaching_table, and round_room.

Folder Icon

Sign up to start coding

Already have an account?