Python short course: Class basics in Python

 Class is crucial for OOP. It can generate many instances of the class object. Think about the Monkey King generating many fake Monkey Kings by pulling his fur in the Journey to the West. Below are a basic class object:

 


In [2]: class ClassTest:
   ...:     def setdata(self, value):
   ...:         self.data = value
   ...:     def display(self):
   ...:         print(self.data)
   ...: 

In [3]: class_test = ClassTest()

In [4]: class_test.setdata("I love Python")

In [5]: class_test.display()
I love Python

In [6]: class_test_2 = ClassTest()

In [7]: class_test_2.setdata(12345)

In [8]: class_test_2.display()
12345

reference: 
Lutz, Mark Learning Python (5th ed.) 2013 O'Reilly: CA p. 1594

Comments

Popular posts from this blog

Lab's weekly topic - week 50 2023 - Detailed explanation for encoder of U-Net

Lab's recommendation: Awesome books to learn machine learning and AI (continue updating)