HITCHHIKER’ GUIDE TO PYTHON NOTE 1

pep8 optparse.py

pip3 install autopep8

autopep8 --in-place optparse.py

 

Namespace Tools

The functions dir(), globals(), and locals() help with quick namespace introspection:

• dir(object) returns a list of attributes that are accessible via the object

• globals() returns a dictionary of the attributes currently in the global namespace,

along with their values.

• locals() returns a dictionary of the attributes in the current local namespace

(e.g., within a function), along with their values.

 

Any directory with an __init__.py file is considered a Python package. The top-level directory with an __init__.py is the root package.9

 

It is normal, even good practice, to leave an __init__.py empty when the package'smodules and subpackages do not need to share any code—the HowDoI and Diamond projects that are used as examples in the next section both have no code except version numbers in their __init__.py files.

 

A decorator is a function or a class method that wraps (or decorates) another function or method. The decorated function or method will replace the original

function or method.

 

Python is dynamically typed (as opposed to statically typed), meaning variables do not have a fixed type. Variables are implemented as pointers to an object, making it possible for the variable a to be set to the value 42, then to the value "thanks for all the fish", then to a function.

 

Mutable types

These allow in-place modification of the object's content. Examples are lists and dictionaries, which have mutating methods like list.append() or dict.pop()

and can be modified in place.

Immutable types

These types provide no method for changing their content. For instance, the variable x set to the integer 6 has no "increment" method. To compute x + 1, you have to create another integer and give it a name.

posted @ 2017-11-14 17:01  niejn  阅读(150)  评论(0编辑  收藏  举报