search
HomeBackend DevelopmentPython Tutorial跟老齐学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__用的比较多,所以前面很多例子都是它。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

For loop and while loop in Python: What are the advantages of each?For loop and while loop in Python: What are the advantages of each?May 13, 2025 am 12:01 AM

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.