Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Private and Public Concepts | Encapsulation
Object-Oriented Programming in Python

bookPrivate and Public Concepts

Encapsulation provides access to the methods and attributes of the class.

Private or public

Some attributes and/or methods cannot be accessed or modified. For example, it is correct not to change the number_of_legs of your cat object (usually, this value is 4).

This attribute must be private - not available outside the class.

By default, all methods and attributes are public in the Python class. To make it private, add two __ before the variable name:

12345678
class Cat: def __init__(self, name = 'Kitty', age = 1): self.name = name self.age = age # Private self.__number_of_legs = 4 print(Cat)
copy

The way to access even the private value:

12345678910
class Cat: def __init__(self, name = 'Kitty', age = 1): self.name = name self.age = age # Private self.__number_of_legs = 4 cat = Cat() # Access the private value print(cat._Cat__number_of_legs)
copy
question mark

How to access the private value?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Pergunte-me perguntas sobre este assunto

Resumir este capítulo

Mostrar exemplos do mundo real

Awesome!

Completion rate improved to 7.69

bookPrivate and Public Concepts

Deslize para mostrar o menu

Encapsulation provides access to the methods and attributes of the class.

Private or public

Some attributes and/or methods cannot be accessed or modified. For example, it is correct not to change the number_of_legs of your cat object (usually, this value is 4).

This attribute must be private - not available outside the class.

By default, all methods and attributes are public in the Python class. To make it private, add two __ before the variable name:

12345678
class Cat: def __init__(self, name = 'Kitty', age = 1): self.name = name self.age = age # Private self.__number_of_legs = 4 print(Cat)
copy

The way to access even the private value:

12345678910
class Cat: def __init__(self, name = 'Kitty', age = 1): self.name = name self.age = age # Private self.__number_of_legs = 4 cat = Cat() # Access the private value print(cat._Cat__number_of_legs)
copy
question mark

How to access the private value?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
some-alt