python變量命名的7條建議
前言
Quora 問答社區(qū)的一個開發(fā)者投票統(tǒng)計,程序員最大的難題是:如何命名(例如:給變量,類,函數(shù)等等),光是如何命名一項的選票幾乎是其它八項的投票結(jié)果的總和。如何給變量命名,如何讓它變得有意義成了程序員不可逾越的難題,這篇文章參考了 Clean Code ,提供7條命名建議,希望能在取名字的過程中給你帶來一些幫助。
以下都是基于Python3.7語法
1、使用有意義而且可讀的變量名
差
ymdstr = datetime.date.today().strftime("%y-%m-%d")
鬼知道 ymd 是什么?
好
current_date: str = datetime.date.today().strftime("%y-%m-%d")
看到 current_date,一眼就懂。
2、同類型的變量使用相同的詞匯
差 這三個函數(shù)都是和用戶相關(guān)的信息,卻使用了三個名字
get_user_info() get_client_data() get_customer_record()
好 如果實體相同,你應(yīng)該統(tǒng)一名字
get_user_info() get_user_data() get_user_record()
極好 因為 Python 是一門面向?qū)ο蟮恼Z言,用一個類來實現(xiàn)更加合理,分別用實例屬性、property 方法和實例方法來表示。
class User: info : str @property def data(self) -> dict: # ... def get_record(self) -> Union[Record, None]: # ...
3、使用可搜索的名字
大部分時間你都是在讀代碼而不是寫代碼,所以我們寫的代碼可讀且可被搜索尤為重要,一個沒有名字的變量無法幫助我們理解程序,也傷害了讀者,記住:確??伤阉鳌?/p>
差
time.sleep(86400);
What the fuck, 上帝也不知道86400是個什么概念
好
# 在全局命名空間聲明變量,一天有多少秒 SECONDS_IN_A_DAY = 60 * 60 * 24 time.sleep(SECONDS_IN_A_DAY)
清晰多了。
4、使用可自我描述的變量
差
address = 'One Infinite Loop, Cupertino 95014' city_zip_code_regex = r'^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$' matches = re.match(city_zip_code_regex, address) save_city_zip_code(matches[1], matches[2])
matches[1] 沒有自我解釋自己是誰的作用
一般
address = 'One Infinite Loop, Cupertino 95014' city_zip_code_regex = r'^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$' matches = re.match(city_zip_code_regex, address) city, zip_code = matches.groups() save_city_zip_code(city, zip_code)
你應(yīng)該看懂了, matches.groups() 自動解包成兩個變量,分別是 city,zip_code
好
address = 'One Infinite Loop, Cupertino 95014' city_zip_code_regex = r'^[^,\\]+[,\\\s]+(?P<city>.+?)\s*(?P<zip_code>\d{5})?$' matches = re.match(city_zip_code_regex, address) save_city_zip_code(matches['city'], matches['zip_code'])
5、 不要強迫讀者猜測變量的意義,明了勝于晦澀
差
seq = ('Austin', 'New York', 'San Francisco') for item in seq: do_stuff() do_some_other_stuff() # ... # Wait, what's `item` for again? dispatch(item)
seq 是什么?序列?什么序列呢?沒人知道,只能繼續(xù)往下看才知道。
好
locations = ('Austin', 'New York', 'San Francisco') for location in locations: do_stuff() do_some_other_stuff() # ... dispatch(location)
用 locations 表示,一看就知道這是幾個地區(qū)組成的元組
6、不要添加無謂的上下文
如果你的類名已經(jīng)可以告訴了你什么,就不要在重復(fù)對變量名進行描述
差
class Car: car_make: str car_model: str car_color: str
感覺畫蛇添足,如無必要,勿增實體。
好
class Car: make: str model: str color: str
7、使用默認參數(shù)代替短路運算和條件運算
差
def create_micro_brewery(name): name = "Hipster Brew Co." if name is None else name slug = hashlib.sha1(name.encode()).hexdigest() # etc.
好
def create_micro_brewery(name: str = "Hipster Brew Co."): slug = hashlib.sha1(name.encode()).hexdigest() # etc.
這個應(yīng)該能理解吧,既然函數(shù)里面需要對沒有參數(shù)的變量做處理,為啥不在定義的時候指定它呢?
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django 接收Post請求數(shù)據(jù),并保存到數(shù)據(jù)庫的實現(xiàn)方法
今天小編就為大家分享一篇Django 接收Post請求數(shù)據(jù),并保存到數(shù)據(jù)庫的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07基于virtualenv創(chuàng)建python虛擬環(huán)境過程圖解
這篇文章主要介紹了基于virtualenv創(chuàng)建python虛擬環(huán)境過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03python?gravis庫實現(xiàn)圖形數(shù)據(jù)可視化實例探索
這篇文章主要為大家介紹了python?gravis庫實現(xiàn)圖形數(shù)據(jù)可視化實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-02-02