Python Technical Notes

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


13. In python3.4 spyder, sys.path[0] will not return the correct path of the module

14. Generator is used to generate a function that behaves like an iterator when called (given an argument in most cases)
      Iterator is an object that implements next() method, next() is expected to return the next element of the iterable object.

15. MySQL client is not a dependance of the installation of MySQLdb. 
      Copy Anaconda folder directly works across different physical machines (windows)

16. The standard return code for a successful SQL query is, based on the official documentation, 0.
      However, the return code for a successful SQL query provided by mysqldb module is 1.

17.  All mutable object in python is passed by reference, therefore every operation performed on a list passed in as a function argument will also affect the global list object.
The id of the function object is the same as its return value

A function is automatically added a return value None by the interpreter if no return statement is specified.

Case A:

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



The above function will always return none, even if the recursive process is done as expected, because every recursive call will return None.

In case A, every call of function foo will return either my_list or None.
In case B, when evaluating return foo(my_list, i), a return value will not be immediately available, but the evaluation process continue  until the condition of return my_list statement is reached.

Case A, recursive function call of foo , return None multiple times, then return my_list at last.
Case B, recursive function call of foo, only returns my_list

18. The default file R/W method in python accepts relative path as the argument, but it should be noted that this relative path is relative to the CWD when execuating the script


19. Underscore Variable in Python

_ has 3 main conventional uses in Python:

  1. 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
  2. For translation lookup in i18n (imported from the corresponding C conventions, I believe)
  3. 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)



21. Class method should be declared in the following format:
class test:
    @classmethod
    def pp(cls):
        print("Class Method Works")

22. python MySQL-db installation bug
Reason: No mysql driver installed on the host system.

23. In python 3:
import statement can accept file type as follows: .py .pyc .so
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值