Skip to content

Python function encapsulation

Python function encapsulation refers to the practice of structuring code so that the internal logic of a function is hidden from the caller, focusing instead on the input and output.^[400-devops-09-scripting-language-python-introduction-readme.md]

Core Concepts

Effective encapsulation relies on defining functions that serve a "single purpose".^[400-devops-09-scripting-language-python-introduction-readme.md] By restricting a function to a single responsibility, the code becomes easier to test and maintain.^[400-devops-09-scripting-language-python-introduction-readme.md]

A key aspect of this design is that the caller does not need to know "how the data is retrieved".^[400-devops-09-scripting-language-python-introduction-readme.md] For example, a function might return data that actually comes from a database or a file system, but to the external code, it simply provides a result without exposing the underlying data provider.^[400-devops-09-scripting-language-python-introduction-readme.md]

Benefits

Functions designed with these characteristics "do not leak" their Data provider, ensuring a clear separation of concerns.[^400-devops-09-scripting-language-python-introduction-readme.md]

  • [[Functions]]
  • [[Data abstraction]]

Sources

^[400-devops-09-scripting-language-python-introduction-readme.md]