- Python Data Structures and Algorithms
- Benjamin Baka
- 217字
- 2021-07-09 19:45:00
Data encapsulation and properties
Unless otherwise specified, all attributes and methods are accessible without restriction. This also means that everything defined in a base class is accessible from a derived class. This may cause problems when we are building object-oriented applications where we may want to hide the internal implementation of an object. This can lead to namespace conflicts between objects defined in derived classes with the base class. To prevent this, the methods we define private attributes with have a double underscore, such as __privateMethod(). These method names are automatically changed to _Classname__privateMethod() to prevent name conflicts with methods defined in base classes. Be aware that this does not strictly hide private attributes, rather it just provides a mechanism for preventing name conflicts.
It is recommended to use private attributes when using a class property to define mutable attributes. A property is a kind of attribute that rather than returning a stored value, computes its value when called. For example, we could redefine the exp() property with the following:
class Bexp(Aexp):
__base=3
def __exp(self):
return(x**cls.base)
In this chapter, we have looked at some of the fundamentals of the Python programming language, from basic operations to functions, classes, and objects in Python. In the next chapter, we will examine, in detail, the built-in data structures of Python.
- The Complete Rust Programming Reference Guide
- Java入門經典(第6版)
- ReSharper Essentials
- HoloLens Beginner's Guide
- 編寫高質量代碼:改善Python程序的91個建議
- Kinect for Windows SDK Programming Guide
- C語言程序設計案例精粹
- Mastering Android Development with Kotlin
- Statistical Application Development with R and Python(Second Edition)
- JavaScript腳本特效編程給力起飛
- Building Serverless Web Applications
- IDA Pro權威指南(第2版)
- OpenCV 3 Blueprints
- Android系統下Java編程詳解
- 零基礎學SQL(升級版)