introduction
A Quick Tutorial on Creating and Using Classes in Python
Table of contents
Creating a Simple Class in Python
A class in Python serves as a blueprint for creating objects. To define a simple class, use the class
keyword.
1 2 | class Member: s = 'Hello world!' |
Here, Member
is a class with a single property s
.
Creating an Instance
To create an instance (object) of the Member
class:
1 | member_01 = Member() |
Accessing Properties
You can access the property s
like this:
1 | print(member_01.s) |
Output:
1 | Hello world! |
Creating an Empty Class
To create a class with no properties or methods, you can use the pass
keyword:
1 2 | class Member: pass |
Using the __init__()
Method to Initialize Properties
The __init__()
method is a constructor that allows you to initialize an object’s properties when it is created. Here’s how:
1 2 3 4 5 | class Member: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age |
Creating and Accessing Objects
Now, when you create an object, you need to provide the required parameters:
1 2 3 4 5 | member_01 = Member("John", "Doe", 24) print(member_01.first_name) # Output: John print(member_01.last_name) # Output: Doe print(member_01.age) # Output: 24 |
You can create multiple objects with different properties:
1 2 3 4 5 | member_02 = Member("Emma", "Watson", 30) print(member_02.first_name) # Output: Emma print(member_02.last_name) # Output: Watson print(member_02.age) # Output: 30 |
Adding Optional Parameters with *args
The *args
keyword allows you to pass a variable number of arguments to a method.
Example:
1 2 3 4 5 6 7 8 | class Member: def __init__(self, first_name, last_name, age, *args): self.first_name = first_name self.last_name = last_name self.age = age if 'Male' in args: self.gender = 'Male' |
Usage:
1 2 3 | member_01 = Member("John", "Doe", 24, 'Male', 'White') print(member_01.gender) # Output: Male |
Adding Optional Parameters with **kwargs
The **kwargs
keyword allows passing named arguments that can be processed as a dictionary.
Example:
1 2 3 4 5 6 | class Member: def __init__(self, first_name, last_name, age, **kwargs): self.first_name = first_name self.last_name = last_name self.age = age self.height = kwargs.get('height', None) # Optional parameter |
Usage:
1 2 3 | member_01 = Member("John", "Doe", 24, height=5.3, eye_color='green') print(member_01.height) # Output: 5.3 |
Defining Object Methods
You can define methods inside a class to perform operations using the object’s data.
Example:
1 2 3 4 5 6 7 8 | class Member: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def full_name(self): return f"Full name: {self.first_name} {self.last_name}" |
Usage:
1 2 | member_01 = Member("John", "Doe", 24) print(member_01.full_name()) # Output: Full name: John Doe |
Deleting an Object
To delete an object, use the del
keyword:
1 | del member_01 |
After deletion, attempting to access member_01
will raise a NameError
.
Creating Child Classes (Inheritance)
Inheriting a Parent Class
A child class inherits all properties and methods from the parent class.
1 2 3 4 5 6 7 8 9 10 11 | class Member: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def full_name(self): return f"Full name: {self.first_name} {self.last_name}" class PremiumMember(Member): pass |
Usage:
1 2 | pm1 = PremiumMember("John", "Doe", 24) print(pm1.full_name()) # Output: Full name: John Doe |
Overriding the Constructor
If a child class defines its own __init__()
method, you must explicitly call the parent’s constructor:
1 2 3 4 5 6 | class PremiumMember(Member): def __init__(self, first_name, last_name, age): super().__init__(first_name, last_name, age) def promo_code(self): return 1234 |
Usage:
1 2 | pm1 = PremiumMember("John", "Doe", 24) print(pm1.promo_code()) # Output: 1234 |
References
Links | Site |
---|---|
Python Classes and Objects | w3schools.com |
How To Use args and *kwargs in Python 3 | digitalocean.com |
Python Official Documentation on Classes | docs.python.org |
Python Inheritance | w3schools |