識符是計算機語言中允許作為名字的有效字符串集合。
Python的標識符,與C語言類似:
Python1.4開始使用的關鍵字:
and | break | class | continue |
def | del | elif | else |
except | exec | finally | for |
from | global | if | import |
in | is | lambda | not |
or | pass | print(本身也是內置函數) | raise |
return | try | while |
除關鍵字外,Python還可以再任何一級代碼中使用“內建”的名字計劃,這些名字可以由解釋器設置或使用。雖然built-in不是關鍵字,但建議把它當作“系統保留字”,不做他用。而在一些特殊情況下,Python也允許這些關鍵字被覆蓋(重新定義、替換)。Python不支持重載,所以任何時刻都只有一個名字綁定。
在進入Python控制臺之前,編譯器加載的內容。可以看做是Python代碼的全局變量。built-in是__builtins__模塊的成員。因此,下述代碼可以打印全部的built-in成員。
builtin_list=dir(__builtins__)
for i in range(len(builtin_list)):
print(builtin_list[i])
輸出結果如下:
輸出(為了控制篇幅將回車換行符替換成了逗號):
ArithmeticError,AssertionError,AttributeError,BaseException,BlockingIOError,BrokenPipeError,BufferError,BytesWarning,ChildProcessError,ConnectionAbortedError,ConnectionError,ConnectionRefusedError,ConnectionResetError,DeprecationWarning,EOFError,Ellipsis,EnvironmentError,Exception,False,FileExistsError,FileNotFoundError,FloatingPointError,FutureWarning,GeneratorExit,IOError,ImportError,ImportWarning,IndentationError,IndexError,InterruptedError,IsADirectoryError,KeyError,KeyboardInterrupt,LookupError,MemoryError,ModuleNotFoundError,NameError,None,NotADirectoryError,NotImplemented,NotImplementedError,OSError,OverflowError,PendingDeprecationWarning,PermissionError,ProcessLookupError,RecursionError,ReferenceError,ResourceWarning,RuntimeError,RuntimeWarning,StopAsyncIteration,StopIteration,SyntaxError,SyntaxWarning,SystemError,SystemExit,TabError,TimeoutError,True,TypeError,UnboundLocalError,UnicodeDecodeError,UnicodeEncodeError,UnicodeError,UnicodeTranslateError,UnicodeWarning,UserWarning,ValueError,Warning,WindowsError,ZeroDivisionError,__build_class__,__debug__,__doc,import__,__loader__,__name__,__package__,__spec__,abs,all,any,ascii,bin,bool,breakpoint,bytearray,bytes,callable,chr,classmethod,compile,complex,copyright,credits,delattr,dict,dir,divmod,enumerate,eval,exec,exit,filter,float,format,frozenset,getattr,globals,hasattr,hash,help,hex,id,input,int,isinstance,issubclass,iter,len,license,list,locals,map,max,memoryview,min,next,object,oct,open,ord,pow,print,property,quit,range,repr,reversed,round,set,setattr,slice,sorted,staticmethod,str,sum,super,tuple,type,vars,zip
可見,Python的內置函數非常豐富,使用得當可以大大提升開發效率,下面就整理部分內置函數進行分類總結。
1)數值運算
2)序列操作
序列操作指的是對列表、元組、字符串等序列類型的操作,Python的內置函數提供了多種序列操作方法,包括以下函數:
3)文件操作
4)字符串操作
5)模塊操作
Python用下劃線作為變量前綴、后綴指定的特殊變量具有特殊含義。
1)_xxx
_xx:這樣的對象叫做保護變量,不能用’from module import *'導入。以單個下劃線開頭命名的方法或者變量,就是說明它是僅提供內部使用的。如下述樣例代碼:
在Demo1中構造兩個方法:
def say_hello():
print("say hello")
def _say_hello():
print("_say hello")
在Demo2中,引入Demo1,直接調用該方法,在比較先進的編譯器中會直接提示未定義_say_hello()方法。即使不提示,運行時也同樣會報錯。
from Demo1 import *
say_hello()
_say_hello()
2)__xxx
__xxx:類中的私有成員,只有類對象自己的函數中能訪問。
下述樣例代碼,請參考注釋。
class A():
def __init__(self):
self.__salary=12000
def afun(self):
# 在類本身的函數中訪問雙下劃線開頭的變量
print(self.__salary)
class Aa(A):
def __init__(self):
super().__init__()
a=A()
aa=Aa()
# 類對象本身調用自己的函數
a.afun() # pass 類可以通過本身的方法訪問自己的成員變量
aa.afun() # pass 子類可以通過繼承的父類方法訪問父類的成員變量
a.__salary # NG 類對象不可以直接通過變量名進行訪問
3)__xxx___
__xxx__系統自定義名稱,都是一些特殊的屬性和方法。被稱為"魔術方法"或"特殊方法"。這些方法和屬性在類定義和實例化過程中有特定的用途。
if __name__=="__main__":
main()
下述樣例代碼可以打印方法的__doc__屬性。輸出結果從略。
class Calculator:
"""
一個簡單的計算器類。
包含加法和減法運算。
"""
def add(self, a, b):
"""返回兩個數的和。"""
return a + b
def subtract(self, a, b):
"""返回兩個數的差。"""
return a - b
def multiply(a, b):
"""
返回兩個數的乘積。
參數:
a -- 第一個數
b -- 第二個數
返回值:
兩個數的乘積
"""
return a * b
if __name__=='__main__':
print(Calculator.add.__doc__) # 輸出:返回兩個數的和。
print(Calculator.__doc__) # 輸出:一個簡單的計算器類。包含加法和減法運算。
print(multiply.__doc__) # 輸出:返回兩個數的乘積。
下述樣例,在mymodule.py中定義各種__doc__屬性。
"""
這是一個示例模塊。
它包含了一些基本的函數。
"""
def foo():
"""這是一個示例函數。"""
return "Hello, World!"
另一個py文件中引用mymodule.py,即可以對該文件內的__doc__屬性進行打印。輸出結果從略。
++標識符是什么,如何認知更有助于理解
要了解或理解C++中的標識符是什么,可以先從“標識符”三個字上進行理解。
首先,“標識”一詞用于修飾“符”,也就是說,標識符追根究底就是一種計算機語言的符號,其主要作用就是用于“標識”,而既然是用于“標識”,而自然就是用于“區分”,比如區分不同的變量、繼而區分不同變量的作用,就像人的名字。
而這些又有什么作用呢?這就可以從標識符的作用來對C++標識符進行理解。
C++的標識符不僅可以用于普通類型變量的命名,即對普通類型的變量進行標識,也可以用于對函數、類、結構體、命名空間、指針等進行標識、進行區分。
而這些標識符又是“為誰”而進行標識區分的呢?
個人認為,除了計算機之外,最主要的應該就是為程序員而進行區分的,目的就是為了增強C++程序的可讀性、可編程性,方便程序員編寫、閱讀、修改、維護、優化等使用。
如上便是個人對C++標識符的一些思考認識。C++標識符的命名規則,可參考章節:C++標識符的作用詳解,及命名規范。
原文:C++標識符是什么
免責聲明:內容僅供參考,不保證正確性。
ag xinchi外語
英 [t?ɡ] 美 [t?ɡ]
n. 標簽,標牌;稱呼,諢名;(電子)追蹤器;帶創作者名字的涂鴉;(計算機)標識符;(兒童玩的)捉人游戲; 附加疑問句;一小部分;(棒球)觸殺;引語,格言,諺語;<美>(機動車的)牌照;一簇糾結的羊毛;(動物顏色鮮艷的)尾尖;(鞋帶上的金屬或塑料)包頭;(戲劇的)終場詞;疊歌,副歌
v. 給……加上標簽;把……稱作;涂鴉; 給(計算機程序或文件)加標記;(在捉人游戲中)觸摸;觸殺(跑壘員);(跑壘員)用腳觸壘;<英,非正式> 尾隨;(尤指沒被邀請而)跟隨(或陪伴);添加;給……裝跟蹤器;給(羊)剪去糾結的毛
[ 復數 tags 第三人稱單數 tags 現在分詞 tagging 過去式 tagged 過去分詞 tagged ]
短語
Tag Heuer 泰格豪雅 ; 豪雅表 ; 瑞士豪雅 ; 豪雅表手表
title tag 標題標簽 ; 頁面標題 ; 頁面題目 ; 題目標簽
Tag Cloud 標簽云 ; 標簽 ; 標簽云圖 ; 標記云
更多網絡短語
柯林斯英漢雙解大詞典
tag /t?ɡ/ CET4 TEM4 ( tagging, tagged, tags )
1. N-COUNT A tag is a small piece of card or cloth which is attached to an object or person and has information about that object or person on it. 標簽
例:Staff wore name tags and called inmates by their first names.
工作人員戴著姓名標簽,對同僚直呼其名。
2. → see also price tag
3. N-COUNT An electronic tag is a device that is firmly attached to someone or something and sets off an alarm if that person or thing moves away or is removed. (電子) 跟蹤器
例:Ranchers are testing electronic tags on animals' ears to create a national cattle-tracking system.
牧場主正在檢測動物耳朵上的跟蹤器,以建立一個全國性的家畜跟蹤系統。
4. N-UNCOUNT Tag is a children's game where one child runs to touch or tag the others. (兒童玩的) 觸碰捉人游戲
5. V-T If you tag something, you attach something to it or mark it so that it can be identified later. 給…貼標簽
例:Professor Orr has developed interesting ways of tagging chemical molecules using existing laboratory lasers.
奧爾教授已經研發出了用現有的實驗室激光給化學分子貼標簽的有趣方法。
詞組短語同近義詞同根詞詞語辨析
price tag 價格標簽;標價
name tag 胸牌;名稱標簽
tag along 緊跟;尾隨
claim tag n. 行李票
tag line n. 時髦用語;收尾語
更多詞組短語
雙語例句原聲例句權威例句
Have you looked at the price tag?
你看過價格標簽了嗎?
youdao
We choose to keep the tag.
我們選擇保留這個標簽。
youdao
It accomplishes this through the tag.
它通過標簽來做到這一點。
每天分享外語知識,請點擊 【關注】,不漏掉任何一期,助你提高外語水平。
【訊馳外語】可樂老師 編輯。
轉載《有道詞典》,如有侵權請通知刪除。
碼字不易,敬請【點贊】!
My email:ilikework_cz@126.com