Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära What is Encapsulation? | Encapsulation
In-Depth Python OOP

bookWhat is Encapsulation?

Encapsulation is one of the fundamental concepts of object-oriented programming (OOP). It refers to the ability to store information inside a class with limited access.

Access Types

There are different access types for attributes and methods:

  • Public: Attributes/Methods that can be accessed and used both inside and outside the class.
  • Protected: Attributes/Methods that are intended to be used within the class and its subclasses. They are indicated by a single underscore prefix, such as _attribute or _method.
  • Private: Attributes/Methods that are intended to be used only within the class and cannot be accessed or used from outside the class. They are indicated by a double underscore prefix, such as __attribute or __method.

Syntax

Access types in Python are implemented simply. You can create attributes/methods with addition marks:

class SomeClass:
    attribute = "Public"
    _attribute = "Protected"
    __attribute = "Private"
    
    def method(self):
        print("Public method")
        
    def _method(self):
        print("Protected method")

    def __method(self):
        print("Private method")
question mark

Choose from the available access types:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 2.78

bookWhat is Encapsulation?

Svep för att visa menyn

Encapsulation is one of the fundamental concepts of object-oriented programming (OOP). It refers to the ability to store information inside a class with limited access.

Access Types

There are different access types for attributes and methods:

  • Public: Attributes/Methods that can be accessed and used both inside and outside the class.
  • Protected: Attributes/Methods that are intended to be used within the class and its subclasses. They are indicated by a single underscore prefix, such as _attribute or _method.
  • Private: Attributes/Methods that are intended to be used only within the class and cannot be accessed or used from outside the class. They are indicated by a double underscore prefix, such as __attribute or __method.

Syntax

Access types in Python are implemented simply. You can create attributes/methods with addition marks:

class SomeClass:
    attribute = "Public"
    _attribute = "Protected"
    __attribute = "Private"
    
    def method(self):
        print("Public method")
        
    def _method(self):
        print("Protected method")

    def __method(self):
        print("Private method")
question mark

Choose from the available access types:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt