Python Class - Create Class Object - Access Properties & Functions


Python - Create Class Object

In this tutorial, we will explore how to create an object from a user-defined class in Python. Let's consider the following example of a Laptop class, where we define properties and methods, and create an instance (object) of this class.

Below is the definition of the Laptop class, which includes properties like name and processor, as well as methods to interact with those properties.

class Laptop:
	name = 'My Laptop'
	processor = 'Intel Core'
	
	@staticmethod
	def start():
		print('Laptop is starting..')
		
	@staticmethod
	def restart():
		print('Laptop is restarting')
		
	def details(self):
		print('My laptop name is:', self.name)
		print('It has',self.processor,'processor.')

Here:

  • name and processor are class properties.
  • start() and restart() are static methods, meaning they can be called without creating an instance of the class.
  • details() is an instance method that requires an object of the class to be invoked.

Now, let's create an object of the Laptop class and use it to access the properties and methods defined.

Python Program

# Define the Laptop class
class Laptop:
	name = 'My Laptop'
	processor = 'Intel Core'
	
	@staticmethod
	def start():
		print('Laptop is starting..')
		
	@staticmethod
	def restart():
		print('Laptop is restarting')
		
	def details(self):
		print('My laptop name is:', self.name)
		print('It has',self.processor,'processor.')

# Create object
laptop1 = Laptop()
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
laptop1.details()

Explanation:

  1. The class Laptop is defined with two properties: name and processor, along with methods to interact with them.
  2. The object laptop1 is created using the class constructor: laptop1 = Laptop(). This initializes a new instance of the Laptop class.
  3. After creating the object, we change the name and processor properties of laptop1.
  4. The details() method is then called to display the properties of laptop1.
  5. In this example, the output will show that the laptop name is Dell Alienware and the processor is Intel Core i7.

Output

My laptop name is: Dell Alienware
It has Intel Core i7 processor.

2. Create Multiple Objects of a Class

In this example, we will create multiple objects of the Laptop class and display their details.

Python Program

# Define the Laptop class
class Laptop:
	name = 'My Laptop'
	processor = 'Intel Core'
	
	@staticmethod
	def start():
		print('Laptop is starting..')
		
	@staticmethod
	def restart():
		print('Laptop is restarting')
		
	def details(self):
		print('My laptop name is:', self.name)
		print('It has',self.processor,'processor.')

# Create multiple objects of the Laptop class
laptop1 = Laptop()
laptop2 = Laptop()

# Assign different properties
laptop1.name = 'HP Spectre'
laptop1.processor = 'Intel Core i5'
laptop2.name = 'MacBook Pro'
laptop2.processor = 'Apple M1'

# Display details
laptop1.details()
laptop2.details()

Explanation:

  1. We create two objects, laptop1 and laptop2, from the same Laptop class.
  2. Each object is assigned different values for the name and processor properties.
  3. We then call the details() method on each object to display their respective properties.
  4. The output will show the details for both laptops.

Output

My laptop name is: HP Spectre
It has Intel Core i5 processor.

My laptop name is: MacBook Pro
It has Apple M1 processor.

3. Using Static Methods with Class Object

In this example, we will demonstrate how to call static methods like start() and restart() on the class directly, without creating an instance.

Python Program

# Define the Laptop class
class Laptop:
	name = 'My Laptop'
	processor = 'Intel Core'
	
	@staticmethod
	def start():
		print('Laptop is starting..')
		
	@staticmethod
	def restart():
		print('Laptop is restarting')
		
	def details(self):
		print('My laptop name is:', self.name)
		print('It has',self.processor,'processor.')

# Call static methods directly from the class
Laptop.start()
Laptop.restart()

Explanation:

  1. Since start() and restart() are static methods, they can be called directly from the class, without creating an instance.
  2. Here, we invoke both methods using Laptop.start() and Laptop.restart().
  3. The output will display the respective messages for starting and restarting the laptop.

Output

Laptop is starting..
Laptop is restarting

Summary

In this tutorial, we learned how to create an object of a user-defined class in Python, access class properties and methods, and use static methods. We also explored how to work with multiple objects from the same class and how static methods can be accessed directly from the class without needing to instantiate an object.




Python Libraries