Зміст курсу
In-Depth Python OOP
In-Depth Python OOP
Class
Class Concept
Imagine a class
as a blueprint or a plan for creating something. Let's say it's a plan for creating different cars.
Class is like a blueprint or an idea
- For instance, think of it as a blueprint for creating a car. In this blueprint, you specify how the car should be: what characteristics it can have and what actions it can perform.
- The blueprint outlines things that can be common to all cars, but specific values may vary.
An object is a real car created based on the plan (class
)
- Every time you create a new car, you are creating a new object based on this plan.
- The object has specific values, but it inherits general characteristics from the plan (
class
).
So, a class
is like a plan or an idea that describes how an object should look. An object is a specific instance created according to that plan, with its unique characteristics but still within the boundaries defined by the plan (class
).
Syntax
Let's examine the syntax of classes in Python. To create a class, you utilize the class
keyword. Each class has its own structure that must be defined and implemented. Let's create an empty class for now.
Let's explore some information about the new class:
class SomeClass: pass print(SomeClass) print(type(SomeClass)) print(type(int))
The new class SomeClass
represents a new data type in our program.
Note
Class names must be written in PascalCase (each word is capitalized without spaces), unlike functions and variables, which are typically written in snake_case.
Instance
Here's the corrected and improved version of the text:
An instance is an object of a specific class. For instance, 15
and 26
are instances of the int
class, which represents integer numbers.
Now, let's create instances of our own class. An instance can be created by invoking the class with parentheses ClassName()
:
class SomeClass: pass; instance = SomeClass() some_variable = SomeClass() print(type(instance)) print(type(some_variable))
SomeClass
represents the blueprint for the object we want to create. The variables instance
and some_variable
hold instances of this class. These instances are independent, allowing us to work with each of them separately.
Дякуємо за ваш відгук!