Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Private | Encapsulation
Quizzes & Challenges
Quizzes
Challenges
/
In-Depth Python OOP

bookPrivate

The private access modifier is used to encapsulate attributes and methods within a class. Private attributes and methods are not accessible to subclasses and are intended to be used only within the class itself. They provide a way to hide implementation details and enforce encapsulation.

12345678910111213
class Parent: __attribute = "Private" def get_from_parent(self): print(self.__attribute) class Child(Parent): def get_from_child(self): print(self.__attribute) instance = Child() instance.get_from_parent() instance.get_from_child() # AttributeError
copy

You can utilize parent methods to access parent private attributes/methods, which helps in reducing dependencies.

Python is a highly flexible programming language, allowing you to access private attributes using the following syntax:

instance._ClassName__attribute

But this is a BAD PRACTICE that specific syntax tells us.

Look at the example:

1234567
class SomeClass: __value = "Privated value" instance = SomeClass() print(instance._SomeClass__value) print(SomeClass._SomeClass__value)
copy
question mark

How to define private attribute?

Select the correct answer

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

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

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

Секція 3. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Запитайте мені питання про цей предмет

Сумаризуйте цей розділ

Покажіть реальні приклади

bookPrivate

Свайпніть щоб показати меню

The private access modifier is used to encapsulate attributes and methods within a class. Private attributes and methods are not accessible to subclasses and are intended to be used only within the class itself. They provide a way to hide implementation details and enforce encapsulation.

12345678910111213
class Parent: __attribute = "Private" def get_from_parent(self): print(self.__attribute) class Child(Parent): def get_from_child(self): print(self.__attribute) instance = Child() instance.get_from_parent() instance.get_from_child() # AttributeError
copy

You can utilize parent methods to access parent private attributes/methods, which helps in reducing dependencies.

Python is a highly flexible programming language, allowing you to access private attributes using the following syntax:

instance._ClassName__attribute

But this is a BAD PRACTICE that specific syntax tells us.

Look at the example:

1234567
class SomeClass: __value = "Privated value" instance = SomeClass() print(instance._SomeClass__value) print(SomeClass._SomeClass__value)
copy
question mark

How to define private attribute?

Select the correct answer

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

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

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

Секція 3. Розділ 4
some-alt