python 类的私有方法例子

#coding=utf-8

class Person(object):
    id=12
    def __init__(self,name):
        self.name=name
        self.__inName='ads'

    def __setId(self,id):
        Person.id=id*2

    def getId(self):
        self.__setId(18)
        return Person.id

p=Person("prel")
print p.getId()
print "call private function from out of class"
print p.__setId()#报错

c:Python27Scripts>python task_test.py
36
call private function from out of class
Traceback (most recent call last):
  File "task_test.py", line 19, in <module>
    print p.__setId()
AttributeError: 'Person' object has no attribute '__setId'

原文地址:https://www.cnblogs.com/xiaxiaoxu/p/8778206.html