class Polo():
def __init__(self, color, size, price=99.00, style=None):
self.color = color
self.size = size
self.price = price
self.style = style
def fold(self):
print("FOLDING THE " + self.color.upper() + " POLO!")
def transfer_to(self, store_name):
print(f"SHIPPING THE {self.color.upper()} POLO TO STORE: '{store_name.upper()}'")Custom Classes
A Class is a representation of one or more objects which share the same or similar properties. Each class is like its own custom data type with attributes, methods, and properties defined by the developer.
Definition
In Python, we define a class using the class keyword, followed by the name of the class in titlecase. The class definition requires a specific function called __init__ to initialize, or create a new member of the object class. A class definition may contain many other methods and properties as well.
Here is a simple example of a custom class definition:
Here we see some example instance methods called fold and transfer_to. All instance methods within a class must take self as their first required parameter. This is a reference to the instance itself.
Initialization
After defining an object class, we can create any number of new members, or “instances”, of that object class.
Although multiple instances can have different values, the attributes and behaviors are shared across all instances.
Here are some initialization and usage examples for the Polo class:
polo_1 = Polo(color="Blue", size="Large", price=4.99)
print(type(polo_1))
polo_2 = Polo(color="Yellow", size="Small")
print(type(polo_2))
polo_3 = Polo(color="Red", size="Large", price=65.00, style="Slim")
print(type(polo_3))<class '__main__.Polo'>
<class '__main__.Polo'>
<class '__main__.Polo'>
Attributes:
print(polo_1.color, polo_1.price)
print(polo_2.color, polo_2.price)
print(polo_3.color, polo_3.price)Blue 4.99
Yellow 99.0
Red 65.0
Methods:
polo_1.fold()
polo_2.fold()
polo_3.fold()FOLDING THE BLUE POLO!
FOLDING THE YELLOW POLO!
FOLDING THE RED POLO!
polo_1.transfer_to("Washington, DC")
polo_2.transfer_to("New York, NY")
polo_3.transfer_to("Boston, MA")SHIPPING THE BLUE POLO TO STORE: 'WASHINGTON, DC'
SHIPPING THE YELLOW POLO TO STORE: 'NEW YORK, NY'
SHIPPING THE RED POLO TO STORE: 'BOSTON, MA'
Decorators and Special Methods
We can use a handful of “special methods” and method decorators to supercharge our classes. Special methods are sometimes known as “dunder methods” because they start and end with double underscores. Decorators are defined with preceding @ character.
The class definition below shows an example of using the __repr__ special method, or “representation” function, which determines how the instance should be displayed when printed. It also implements the __iter__ method, which determines what is returned when the item is converted to a dictionary.
Common decorators include:
@property: when you want to invoke the method as a noun, without trailing parentheses@classmethod: when you want to invoke the method on the class itself, instead of on an instance of that class@staticmethod: when the method doesn’t need any information about the instance (i.e. no references toself)
This class uses the @property decorator, which allows us to invoke a given method without trailing parentheses (e.g. team.full_name instead of team.full_name()). This is basically a stylistic choice on our part, enabling us to use properties to represent nouns, and methods to represent verbs.
class Team():
def __init__(self, city, name):
self.city = city
self.name = name
def __repr__(self):
return f"<Team '{self.name}'>"
def __iter__(self):
yield "city", self.city
yield "name", self.name
@property
def full_name(self):
return self.city + " " + self.name
def advertise(self):
print("COME TO", self.city.upper(), "TO SEE OUR GAMES!")Here are some initialization and usage examples for the Team class:
team1 = Team(city="Seattle", name="Storm")
team2 = Team(city="Connecticut", name="Sun")
print(type(team1))
print(type(team2))<class '__main__.Team'>
<class '__main__.Team'>
Special methods:
print(team1)
print(dict(team1))
print(team2)
print(dict(team2))<Team 'Storm'>
{'city': 'Seattle', 'name': 'Storm'}
<Team 'Sun'>
{'city': 'Connecticut', 'name': 'Sun'}
Properties:
print(team1.full_name)
print(team2.full_name)Seattle Storm
Connecticut Sun
Normal methods:
team1.advertise()
team2.advertise()COME TO SEATTLE TO SEE OUR GAMES!
COME TO CONNECTICUT TO SEE OUR GAMES!
Additional Resources
Reference:
- https://docs.python.org/3/tutorial/classes.html
- https://docs.python.org/3/tutorial/classes.html#class-objects
- https://www.w3schools.com/python/python_classes.asp
- https://www.tutorialspoint.com/python/python_classes_objects.htm
- https://realpython.com/python3-object-oriented-programming
- https://realpython.com/instance-class-and-static-methods-demystified