在任何语言中,都会规定某些对象(属性、方法、函数、类等)只能够在某个范围内访问,出了这个范围就不能访问了。这是“公”、“私”之分。此外,还会专门为某些特殊的东西指定一些特殊表示,比如类的名字就不能用class,def等,这就是保留字。除了保留字,python中还为类的名字做了某些特殊准备,就是“专有”的范畴。
私有函数
在某些时候,会看到有一种方法命名比较特别,是以“__”双划线开头的,将这类命名的函数/方法称之为“私有函数”。
所谓私有函数,就是:
私有函数不可以从它们的模块外面被调用
私有类方法不能够从它们的类外面被调用
私有属性不能够从它们的类外面被访问
跟私有对应的,就是所谓的公有啦。有的编程语言用特殊的关键词来说明某函数或方法或类是私有还是公有。但是python仅仅用名字来说明,因为python深刻理解了2k年前孔先生丘所说的“名不正言不顺”的含义。
如果一个 Python 函数,类方法,或属性的名字以两个下划线开始 (但不是结束),它是私有的;其它所有的都是公有的。类方法或者是私有 (只能在它们自已的类中使用) 或者是公有 (任何地方都可使用)。例如:
class Person:
def __init__(self,name):
self.name = name
def __work(self,salary):
print "%s salary is:%d"%(self.name,salary)
这里边定义的方法__work()就是一个私有方法。
下面把上面的类进行完善,然后运行,通过实例来调用这个私有方法
#!/usr/bin/env python
#coding:utf-8
class Person:
def __init__(self,name):
self.name = name
print self.name
def __work(self,salary):
print "%s salary is: %d"%(self.name,salary)
if __name__=="__main__":
officer = Person("Tom")
officer.__work(1000)
#运行结果
Tom
Traceback (most recent call last):
File "225.py", line 14, in
officer.__work(1000)
AttributeError: Person instance has no attribute '__work'
从运行结果中可以看出,当运行到officer.__work(1000)的时候,报错了。并且从报错信息中说,没有该方法。这说明,这个私有方法,无法在类意外调用(其实类意外可以调用私有方法,就是太麻烦,况且也不提倡,故本教程滤去)。
下面将上述代码进行修改,成为:
#!/usr/bin/env python
#coding:utf-8
class Person:
def __init__(self,name):
self.name = name
print self.name
def __work(self,salary):
print "%s salary is: %d"%(self.name,salary)
def worker(self):
self.__work(500) #在类内部调用私有方法
if __name__=="__main__":
officer = Person("Tom")
#officer.__work(1000)
officer.worker()
#运行结果
Tom
Tom salary is: 500
结果正是要得到的。看官是否理解私有方法的用法了呢?
专有方法
如果是以双划线开头,但不是以它结尾,所命名的方法是私有方法;
如果以双划线开头,并且以双划线结尾,所命名的方法就是专有方法。
这是python规定的。所以在写程序的时候要执行,不执行就是跟python过不去,过不去就报错了。
比如前面反复提到的__init__(),就是一个典型的专有方法。那么自己在写别的方法时,就不要用__开头和结尾了。虽然用了也大概没有什么影响,但是在可读性上就差很多了,一段程序如果可读性不好,用不了多长时间自己就看不懂了,更何况别人呢?
关于专有方法,出了__init__()之外,还有诸如:__str__,__setitem__等等,要向看,可以利用dir()函数在交互模式下看看某个函数里面的专有东西。当然,也可以自己定义啦。
因为__init__用的比较多,所以前面很多例子都是它。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。