N
N
Nahs2015-11-23 17:50:16
Python
Nahs, 2015-11-23 17:50:16

How to correctly encapsulate methods in a class?

What would be the correct way to write a class in Python3?
class Classname():

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nirvimel, 2015-11-23
@Nahs

https://www.python.org/dev/peps/pep-0008/#method-n...

class ClassName(object):
    def public_method(self):
        pass

    def _protected_method(self):
        pass

    def __private_method(self):
        pass

S
s0ci0pat, 2015-11-23
@s0ci0pat

Encapsulation in Python only works at the level of an agreement between programmers about which attributes are public and which are internal.
A single underscore at the beginning of an attribute name indicates that the variable or method is not intended to be used outside of class methods, but the attribute is available by that name.
A double underscore at the beginning of an attribute name provides more protection: the attribute becomes inaccessible by that name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question