This is the 10th in a series of Python tutorials; for more, visit my Python collection!
Only classes and objects are introduced here, self, properties, methods, access control, class inheritance, and method rewriting are covered in a later article
In Python, classes and objects are the foundation of object-oriented programming.
1 The Concept of Classes
A class is a blueprint or template for creating objects. It defines a set of attributes (variables) and methods (functions) that describe what characteristics and behaviors an object of that class should have.
2 Defining a Class
In Python, you can use theclass
keyword to define a class. For example, define a class namedPerson
of simple classes:
class Person:
def __init__(self, name, age):
= name
= age
def display(self):
print(f"Name: {}, Age: {}")
-
__init__
method is a special method, called a constructor, used to initialize a new instance of the class. -
self
The parameter represents the instance of the class itself and is the first argument to any method of the class. - class methods need to be passed through the
self
to access class attributes.
3 Creating Objects
Creating an instance (i.e., an object) of a class is very simple. Simply call the class name followed by a pair of parentheses:
person1 = Person("Alice", 25)
person2 = Person("Bob", 30)
4 Accessing Properties and Methods
Properties and methods of an object can be accessed through dot notation:
()
# Output: Name: Alice, Age: 25
print(f "Name: {}, Age: {}")
# Output: Name: Bob, Age: 30
Another awesome Python operation is the ability to directly assign a property to an object that doesn't exist in the class, such as when I assigned gender, a property that doesn't exist in person2:
= "male"
print(getattr(person2, "gender"))
# Output: male
5 Comparison with Java Classes
5.1 Classes and Documents
In Python, a file can contain multiple classes. We'll create a new file, create two classes in it, and use them in the file.
class MyClass1:
def __init__(self, value):
= value
def display(self):
print("MyClass1 Value is:", )
class MyClass2:
def __init__(self, value):
= value
def display(self):
print("MyClass2 Value is:", )
You can import and use these classes in another file:
from example import MyClass1, MyClass2
# establishMyClass1practical example
instance1 = MyClass1("AAA")
()
# exports:MyClass1 Value is: AAA
# establishMyClass2practical example
instance2 = MyClass2("BBB")
()
# exports:MyClass2 Value is: BBB
Java A file can also have multiple classes, but there can only be one public class, and the public class name must be consistent with the file name. This is a basic requirement of the compiler. Although there can be non-public classes, they cannot be used externally, but only in the classes of this file.
5.2 Constructors
The first argument to a Python constructor is self, which represents the instance of the class itself, and self is the first argument to any method of the class. Arguments in the constructor that are assigned with = xx are properties of the class; you don't have to declare them first, as you do in Java.