Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Class | OOP Concepts
In-Depth Python OOP
course content

Зміст курсу

In-Depth Python OOP

In-Depth Python OOP

1. OOP Concepts
2. Inheritance
3. Encapsulation
4. Polymorphism and Abstraction
5. Magic Methods

book
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:

123456
class SomeClass: pass print(SomeClass) print(type(SomeClass)) print(type(int))
copy

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():

12345678
class SomeClass: pass; instance = SomeClass() some_variable = SomeClass() print(type(instance)) print(type(some_variable))
copy

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.

What is Class?

What is Class?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt