Book Image

Expert Python Programming

By : Tarek Ziadé
Book Image

Expert Python Programming

By: Tarek Ziadé

Overview of this book

<p>Python is a dynamic programming language, used in a wide range of domains by programmers who find it simple, yet powerful. From the earliest version 15 years ago to the current one, it has constantly evolved with productivity and code readability in mind.<br /><br />Even if you find writing Python code easy, writing code that is efficient and easy to maintain and reuse is not so straightforward. This book will show you how to do just that:&nbsp; it will show you how Python development should be done. Python expert Tarek Ziadé takes you on a practical tour of Python application development, beginning with setting up the best development environment, and along the way looking at agile methodologies in Python, and applying proven object-oriented principles to your design.</p>
Table of Contents (21 chapters)
Credits
Foreword
About the Author
About the Reviewers
Preface
Index

Descriptors and Properties


When many C++ and Java programmers first learn Python, they are surprised by Python's lack of a private keyword. The nearest concept is 'name mangling'. Every time an attribute is prefixed by "__", it is renamed by the interpreter on the fly:

>>> class MyClass(object):
...     __secret_value = 1
...
>>> instance_of = MyClass()
>>> instance_of.__secret_value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'MyClass' object has no attribute '__secret_value'
>>> dir(MyClass)
['_MyClass__secret_value', '__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
>>> instance_of._MyClass__secret_value
1

This is provided to avoid name collision under inheritance, as the attribute is renamed with the class name as a prefix. It is not...