Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Protected | Encapsulation
In-Depth Python OOP

bookProtected

The protected access modifier can be used in the global scope, but it is generally considered to be a bad practice. IDEs typically hide protected attributes and methods from outside the class, which can hinder autocomplete and code correction.

1234567891011121314151617
class SomeClass(): first = "Something" _attribute = "Protected" def _method(self): print("You should use protected inside the class") def info(self): print("INFO") print("first:", self.first) print("_attribute:", self._attribute) self._method() instance = SomeClass() print("Outside:", instance._attribute) # BAD PRACTICE instance.info() # It's OK
copy

You can use protected attributes and methods inside subclasses.

123456789
class User: _entity = "Internet User" class Admin(User): def print_entity(self): print(self._entity) admin = Admin() admin.print_entity()
copy

The protected access modifier allows you to define logic inside a class that can be extended to subclasses but should not be accessed directly from outside the class hierarchy.

Note

Protected attributes/methods in Python work similarly to regular attributes/methods, but by convention among developers, they are used for encapsulating data within a class and its subclasses.

question mark

How to define protected attribute?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 2.78

bookProtected

Sveip for å vise menyen

The protected access modifier can be used in the global scope, but it is generally considered to be a bad practice. IDEs typically hide protected attributes and methods from outside the class, which can hinder autocomplete and code correction.

1234567891011121314151617
class SomeClass(): first = "Something" _attribute = "Protected" def _method(self): print("You should use protected inside the class") def info(self): print("INFO") print("first:", self.first) print("_attribute:", self._attribute) self._method() instance = SomeClass() print("Outside:", instance._attribute) # BAD PRACTICE instance.info() # It's OK
copy

You can use protected attributes and methods inside subclasses.

123456789
class User: _entity = "Internet User" class Admin(User): def print_entity(self): print(self._entity) admin = Admin() admin.print_entity()
copy

The protected access modifier allows you to define logic inside a class that can be extended to subclasses but should not be accessed directly from outside the class hierarchy.

Note

Protected attributes/methods in Python work similarly to regular attributes/methods, but by convention among developers, they are used for encapsulating data within a class and its subclasses.

question mark

How to define protected attribute?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
some-alt