YouTip LogoYouTip

Python3 Class

# Python3.x Python3 Object-Oriented Programming Python has been an object-oriented language from the very beginning. Because of this, creating classes and objects in Python is very easy. This chapter will provide a detailed introduction to object-oriented programming in Python. If you haven't encountered object-oriented programming languages before, you might need to first understand some basic characteristics of object-oriented languages to form a basic concept of object-oriented programming in your mind. This will make it easier for you to learn Python's object-oriented programming. Next, let's briefly understand some basic characteristics of object-oriented programming. * * * ## Introduction to Object-Oriented Technology * **Class (Class):** A collection used to describe objects with the same attributes and methods. It defines the attributes and methods shared by each object in the collection. An object is an instance of a class. * **Method:** A function defined within a class. * **Class Variable:** A class variable is shared among all instantiated objects. Class variables are defined within the class but outside of any function body. Class variables are typically not used as instance variables. * **Data Member:** Class variables or instance variables used to handle data related to the class and its instance objects. * **Method Overriding:** If a method inherited from a parent class does not meet the needs of a subclass, it can be rewritten. This process is called method overriding (or method overriding). * **Local Variable:** A variable defined within a method, which only has scope within the current instance of the class. * **Instance Variable:** In a class declaration, attributes are represented by variables, which are called instance variables. An instance variable is a variable decorated with `self`. * **Inheritance:** A derived class inherits the fields and methods of a base class. Inheritance also allows treating an object of a derived class as an object of a base class. For example, consider a design where an object of type `Dog` is derived from the `Animal` class, simulating an "is-a" relationship (e.g., a Dog is an Animal). * **Instantiation:** Creating an instance of a class, a concrete object of the class. * **Object:** An instance of a data structure defined by a class. An object includes two types of data members (class variables and instance variables) and methods. Compared to other programming languages, Python adds class mechanisms while trying as much as possible not to introduce new syntax and semantics. Classes in Python provide all the basic features of object-oriented programming: the class inheritance mechanism allows for multiple base classes, derived classes can override any method in the base class, and methods can call methods with the same name in the base class. Objects can contain any number and type of data. ## Class Definition The syntax format is as follows: class ClassName: . . . After a class is instantiated, its attributes can be used. In fact, after creating a class, you can access its attributes through the class name. ## Class Objects Class objects support two operations: attribute references and instantiation. Attribute references use the same standard syntax as all attribute references in Python: **obj.name**. After a class object is created, all names in the class namespace are valid attribute names. So if the class definition is like this: ## Example (Python 3.0+) #!/usr/bin/python3 class MyClass: """A simple class instance"""i = 12345 def f(self): return'hello world'# Instantiate the class x = MyClass()# Access class attributes and methods print("MyClass class attribute i is:", x.i)print("MyClass class method f output is:", x.f()) The above code creates a new class instance and assigns that object to the local variable `x`, which is an empty object. Executing the above program produces the following output: MyClass class attribute i is: 12345MyClass class method f output is: hello world * * * A class has a special method called `__init__()` (**constructor**), which is automatically called when the class is instantiated, like this: def __init__ (self): self.data = [] The class defines the `__init__()` method, and the class instantiation operation will automatically call the `__init__()` method. For example, when instantiating the class `MyClass` as follows, the corresponding `__init__()` method will be called: x = MyClass() Of course, the `__init__()` method can have parameters, which are passed to the class instantiation operation through `__init__()`. For example: ## Example (Python 3.0+) #!/usr/bin/python3 class Complex: def __init__ (self, realpart, imagpart): self.r = realpart self.i = imagpart x = Complex(3.0, -4.5)print(x.r, x.i)# Output: 3.0 -4.5 ### `self` represents the instance of the class, not the class itself The only special difference between a class method and a regular function is that they must have an extra **first parameter name**. By convention, its name is `self`. class Test: def prt(self): print(self)print(self. __class__ )t = Test()t.prt()
← Python3 Errors ExecptionsProp Param Value β†’