# Python 底線 ###### tags: `Python` ### 1. foo_ #### 簡述 避免使用關鍵字(keyword) ```python= list_=[1,2,3]#這樣就不會用到list 這個關鍵字 ``` ## ### 2. _foo #### 簡述 有點像.gitignore的概念 ```python= ------------------------------- file1.py def func(): print("1") def _func(): print("2") ------------------------------- from file1 import * func() _func() -----output----- 1 Error ------------------------------- from file1 import func,_func func() _func() -----output----- 1 2 ``` ## ### 3. __ foo __ #### 簡述 簡單來說 有點像python developer設的變數 如果不怕跟他們衝到就可以用也沒關係 ```python= __init__='hi' print(__init__) -----output----- Error ``` ## ### 4.__foo <-兩個底線 #### 簡述 避免被繼承之後命名的變數重複 ```python= class Jason: location = 'Taipei' hate = 'Python' __wife = 'Mary' def profile(self): """Print my personal profile.""" print(f''' I live in {self.location} I hate {self.favorite_movie} My wife is {self.__wife} ''') Jason.profile() -----output----- I live in Taipei I hate Python My wife is Mary ``` ```python= class Alex(Jason): location = 'Chiayi' __wife = None Alex.profile() -----output----- I live in Chiayi I hate Python My wife is Mary <- no change ``` ```python= clone_person=Jason() print(clone_person.location) print(clone_person.__wife) -----output----- Taipei Error ------------------------------- print(clone_person._Jason__wife) -----output----- Mary ```