next up previous contents
Next: python 運算子 Up: Python Previous: Python 變數   Contents

單底線及雙底線

  1. _single_leading_underscore: Python 沒有真正的 private methods (只能函式內使用的 methods),所以使用一個底線 _ 開頭的 method 表示其他函式不能存取這個 method。

    1. 例如:mypy.py 有兩個 classes myPy, _myPy,在 mypy.py 的程式中直接呼叫都可以使用。
      [dywang@dyw219 zzz]$ vim mypy.py
      [dywang@dyw219 zzz]$ cat mypy.py
      #!/usr/bin/python
      # coding: utf-8
      
      class _myPy1:
      	def __init__(self):
      		print "my first python myPy1"
      class myPy:
      	def __init__(self):
      		print "my first python myPy"
      
      if __name__ == "__main__":
      	print __name__
      	myPy()
      	_myPy1()
      else:
      	print __name__
      
      [dywang@dyw219 zzz]$ ./mypy.py 
      __main__
      my first python myPy
      my first python myPy1
      
    2. test.py 使用 form mypy import * 從檔案 mypy 匯入所有 classes,class myPy 執行成功,但 _myPy1 卻無法執行,也就是無法匯入以底線開頭的 class _myPy1
      [dywang@dyw219 zzz]$ vim test.py 
      [dywang@dyw219 zzz]$ cat test.py 
      #!/usr/bin/python
      
      from mypy import *
      
      myPy.__name__
      myPy()
      _myPy1()
      
      [dywang@dyw219 zzz]$ ./test.py 
      mypy
      my first python myPy
      Traceback (most recent call last):
        File "./test.py", line 7, in <module>
          _myPy1()
      NameError: name '_myPy1' is not defined
      
  2. single_trailing_underscore_: 使用單底線在名稱後面,用來避免與 Python 的 keyword 搞混。例如:class_
      Tkinter.Toplevel(master, class_='ClassName')
    
  3. __double_leading_underscore: 在一個 class 中 method 使用雙底線在名稱前面,且最後沒有底線。例如:class myPy 中的 method __method,則其全名為 _myPy__method
    [dywang@dyw219 zzz]$ vim mypy.py
    [dywang@dyw219 zzz]$ cat mypy.py
    #!/usr/bin/python
    # coding: utf-8
    
    class _myPy1:
    	def __init__(self):
    		print "my first python myPy1"
    class myPy:
    	def __init__(self):
    		print "my first python myPy"
    	def __method(self):
    		print "method in class myPy"
    
    if __name__ == "__main__":
    	print __name__
    	myPy()._myPy__method()
    	_myPy1()
    else:
    	print __name__
    
    [dywang@dyw219 zzz]$ ./mypy.py 
    __main__
    my first python myPy
    method in class myPy
    my first python myPy1
    
  4. __double_leading_and_trailing_underscore__: 名稱前後都有雙底線為特殊物件 (magic object),例如:__init__, __name__,不要自已產生這樣的變數。
    1. mypy.py 程式判斷變數 __name__ 是否為 __main__,也就是執行主程式,不是由其他程式呼叫。
      [dywang@dyw219 zzz]$ vim mypy.py
      [dywang@dyw219 zzz]$ cat mypy.py
      #!/usr/bin/python
      # coding: utf-8
      
      class _myPy1:
      	def __init__(self):
      		print "my first python myPy1"
      class myPy:
      	def __init__(self):
      		print "my first python myPy"
      	def __method(self):
      		print "method in class myPy"
      
      if __name__ == "__main__":
      	print __name__
      	myPy()._myPy__method()
      	_myPy1()
      else:
      	print __name__
      
      [dywang@dyw219 zzz]$ ./mypy.py 
      __main__
      my first python myPy
      method in class myPy
      my first python myPy1
      
    2. test1.py 程式匯入 class myPy,執行__myPy.__name__,則此時的變數 __name__mypy,也就是匯入模組的檔案名稱。
      [dywang@dyw219 zzz]$ vim test1.py 
      [dywang@dyw219 zzz]$ cat test1.py 
      #!/usr/bin/python
      
      from mypy import *
      
      myPy.__name__
      
      [dywang@dyw219 zzz]$ ./test1.py 
      mypy
      



2017-06-14