How to define private method in class with Python

2 Answers

0 votes
class Worker:
    name = ""

    def __init__(self, name):
        self.name = name

    def show(self):
        print("the name is: " + self.name)

    def __private_method_test(self):
        print("__private_method_test")

tom = Worker("Tom")
tom.show()
# private method cannot be called directly, only from within the class.
# tom.__private_method_test()
 
'''
run:
 
the name is: Tom
 
'''

 



answered Jun 19, 2018 by avibootz
0 votes
class Worker:
    name = ""

    def __init__(self, name):
        self.name = name

    def show(self):
        print("the name is: " + self.name)
        self.__private_method_test()

    def __private_method_test(self):
        print("__private_method_test")

tom = Worker("Tom")
tom.show()
# private method cannot be called directly, only from within the class.
# tom.__private_method_test()
 
'''
run:
 
the name is: Tom
__private_method_test
 
'''

 



answered Jun 19, 2018 by avibootz

Related questions

...