Python3 Object-Oriented |
-- Learning not just skills, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Python 3 Tutorial
Python3 Tutorial Python3 Introduction Python3 Environment Setup Python3 VScode Python3 Basic Syntax Python3 Basic Data Types Python3 Data Type Conversion Python3 Interpreter Python3 Comments Python3 Operators Python3 Numbers Python3 Strings Python3 Lists Python3 Tuples Python3 Dictionaries Python3 Sets Python3 Conditional Statements Python3 Loops Python3 First Program Python3 Comprehensions Python3 Iterators & Generators Python3 with Python3 Functions Python3 lambda (Anonymous Functions) Python Decorators Python3 Data Structures Python3 Modules Python __name__ Python3 Input & Output Python3 File Python3 OS Python3 Errors & Exceptions Python3 Object-Oriented Python3 Namespace/Scope Python Virtual Environment Creation Python Type Hints Python3 Standard Library Overview Python3 Examples Python Quiz
Python3 Advanced Tutorial
Python3 Regular Expressions Python3 CGI Programming Python MySQL - mysql-connector Driver Python3 MySQL Database Connection - PyMySQL Driver Python3 Network Programming Python3 SMTP Sending Email Python3 Multithreading Python3 XML Processing Python3 JSON Data Parsing Python3 Date & Time Python3 Built-in Functions Python MongoDB Python3 urllib Python uWSGI Installation & Configuration Python3 pip Python3 operator Module Python math Module Python requests Module Python random Module Python OpenAI Python Useful Resources Python AI Drawing Python statistics Module Python hashlib Module Python Quantitative Python pyecharts Module Python selenium Library Python Web Scraping - BeautifulSoup Python Scrapy Library Python Markdown to HTML Python sys Module Python Pickle Module Python subprocess Module Python queue Module Python StringIO Module Python logging Module Python datetime Module Python re Module Python csv Module Python threading Module Python asyncio Module Python PyQt Python for Loop Python while Loop
Deep Dive
Software
Scripting Languages
Web Design & Development
Programming Languages
Scripts
Development Tools
Web Services
Computer Science
Web Service
Programming
Python3.x Python3 Object-Oriented
Python has been an object-oriented language from its inception. Because of this, creating classes and objects in Python is straightforward. 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 understand some basic characteristics of object-oriented languages first. Forming a basic concept of object-oriented programming in your mind 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: 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 inside a class.
- Class Variable: A variable that is shared among all instances of a class. It is defined within the class but outside 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 redefinition).
- Local Variable: A variable defined within a method that only applies to the current instance of the class.
- Instance Variable: In a class declaration, attributes are represented by variables. These variables are called instance variables, which are variables 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 the base class. For example, consider a design where a
Dogtype object is derived from anAnimalclass, 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 to minimize new syntax and semantics.
Classes in Python provide all the basic functionality 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 the same-named method in the base class.
Objects can contain any amount and type of data.
Class Definition
The syntax format is as follows:
class ClassName:
<statement-1>
.
.
.
<statement-N>
After a class is instantiated, its attributes can be used. In fact, after creating a class, its attributes can be accessed via the class name.
Class Objects
Class objects support two operations: attribute reference and instantiation.
Attribute reference uses 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 example"""
i = 12345
def f(self):
return 'hello world'
# Instantiate the class
x = MyClass()
# Access the class's 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 outputs:
MyClass class attribute i is: 12345
MyClass 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, instantiating the class MyClass will call the corresponding __init__() method:
x = MyClass()
Of course, the __init__() method can have parameters, which are passed to the class instantiation operation via __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 result: 3.0 -4.5
self Represents the Class Instance, Not the Class
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()
YouTip