跟老齐学Python之私有函数和专有方法
在任何语言中,都会规定某些对象(属性、方法、函数、类等)只能够在某个范围内访问,出了这个范围就不能访问了。这是“公”、“私”之分。此外,还会专门为某些特殊的东西指定一些特殊表示,比如类的名字就不能用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__用的比较多,所以前面很多例子都是它。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.
