1. Single Underscore / Double Underscore Included in a Identifier:
An identifier with a leading single underscore is weakly private.
An identifier with a leading double underscore is name mangling.
Example:
>>> class MyClass():
... def __init__(self):
... self.__superprivate = "Hello"
... self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}
2. double leading and double trailing underscores:
__init__, __dict__ are magic built-in names defined by python interpreter, never use any double leading and double trailing names them as user defined identifiers
3. __dict__
This is an universial attribute of many python object, which carries a dictionary object that describes all the attributes of the object.
4. Python Class Decleartion:
The first argument of the __init__ method can be defined as any identifier, but it would be implicitly referred to the instance of this class.
The first argument of the classmethod of a class can be defined as any identifier, but it would be implicitly referred to as the identifier of the class object itself.
5. Python module system
The object defined inside the __init__.py file of a module is equivalent to the .py file model when being imported
6. Python combine two dictionaries
A = dict()
B = dict()
C = A.copy()
C.update(B)
7. str.replace str.split() and str.strip() all accept a string argument, not a regex pattern
8. Pypi Source:
http://pypi.douban.com/simple
<span style="font-family: Arial, Helvetica, sans-serif; line-height: 24.0499992370605px;">https://pypi.python.org/pypi </span>
<span style="font-family:Arial, Helvetica, sans-serif;">9. Pip Using other Pypi sources</span>
<span style="font-family:Arial, Helvetica, sans-serif;">pip install scrapy-redis -i http://pypi.douban.com/simple
</span>
<span style="font-family:Arial, Helvetica, sans-serif;">10. Pip upgrade</span>
<span style="font-family:Arial, Helvetica, sans-serif;">pip install -U scrapy-redis -i </span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 24.0499992370605px;">http://pypi.douban.com/simple</span>
</pre><pre code_snippet_id="232908" snippet_file_name="blog_20140313_1_695010" name="code" class="python" style="margin-top: 0px; margin-bottom: 10px; font-size: 13px; line-height: 24.0499992370605px; background-color: rgb(255, 255, 255);">9. In python2.7, when using json to handle json string containing non-ASCII characters, json.dumps(obj, ensure_ascii=False) must be used to write unicode strings. Also, json.loads only accepts unicode string, which means when reading json files, codecs.open method must always be used.
10. Non-ASCII character literal:
for unicode (utf-8):
- Anything up to U+007F takes 1 byte: Basic Latin
- Then up to U+07FF it takes 2 bytes: Greek, Arabic, Cyrillic, Hebrew, etc
- Then up to U+FFFF it takes 3 bytes: Chinese, Japanese, Korean, Devanagari, etc
- Beyond that it takes 4 bytes
8 bit = 1 Byte
1 Byte = 2 Hex
1 Hex = 4 bits
'\xe9\x98\xbf' HEX value representation for python2.7 string object, in here 6 hex values represents 3 Bytes, and the number 3 may corresponds to the fact that chinese characters takes 3 bytes in unicode encoding.
u'\u963f' Unicode escape for python2.7 unicode object
11. Python function local variable
When a identifier is being referenced in the local scope of a function, it first check if there is a local variable with the same name, if not, then it check if there is a global variable with the same name. However, if a function needs to change the value of a global variable from inside the function, "global" keyword must be used to declare this identifier inside the function.
12. Python27 string translate can only accept 256-string, it cann't handle unicode
13. In python3.4 spyder, sys.path[0] will not return the correct path of the module
14.
脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器;
#!/usr/bin/env python这种用法是为了防止操作系统用户没有将python装在默认的/usr/bin路径里。当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。
#!/usr/bin/python相当于写死了python路径;
#!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法
15. Python Import:
In fact:
Search Path Order: 1. path of the script being executed (Entry Point, regardless of the CWD ) ---> Relative Import (Implicit and Explicit)
2. Paths containing in python interpreter variable: PYTHONPATH ---> Absolute Import
3. Installation Defaults (Don't know what this is )
ALL path above from three sources (1,2,3) are contained in sys.path variable
In appearance:
Absolute Import # needs to put package into the site-package director or modify PYTHONPATH
Explicit Relative Import # Discoverged, Can only be used inside a package (which means the path of the package must be put in pythonpth, or the package must be put into site-packages)
Implicit Relative Import # need to put the imported file in the same directory with the running script, cann't import modules in the upper level
When a module inside a package wants to import another module in the higher level package, only absolute import or explicit relative import can be used (from .. import xxx), the path of the package must be put in pythonpth, or the package must be put into site-packages.
Because explicit relative import can only work when parent directory is known, an standalone execution of a module containing explicit import will raise an import error
16. Py27, Int division
In python 27, int / int will always return an int rounded from the actual float result. float / float will get float, so does int / float and float / int.
In python 34, rounded int division can be achieved by using "//" operator
def foo(my_list, i):
i -= 1
if i > 0:
print(id(my_list))
foo(my_list, i)
# return foo(my_list, i)
else:
return my_list
Case B:
def foo(my_list, i):
i -= 1
if i > 0:
print(id(my_list))
# foo(my_list, i)
return foo(my_list, i)
else:
return my_list
_
has 3 main conventional uses in Python:
- To hold the result of the last executed statement in an interactive interpreter session. This precedent was set by the standard CPython interpreter, and other interpreters have followed suit
- For translation lookup in i18n (imported from the corresponding C conventions, I believe)
- As a general purpose "throwaway" variable name to indicate that part of a function result is being deliberately ignored
The latter two purposes can conflict, so it is necessary to avoid using _
as a throwaway variable in any code block that also uses it for i18n translation.
20. Get all the method and attribute of an object
dir(class_name)
@classmethod
def pp(cls):
print("Class Method Works")