Preview lessons, content and tests

Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.

2. Searching for something specific? See our text overview of all tests. Scroll right for levels, and lists.

3. Student and Teacher User Guides |  Schemes of Work |   Real Teacher use Videos |


Join 36000+ teachers and students using TTIO.

Public and Private atttributes

Also see the unit on 'Encapsulation'. What is the difference between public and private in OOP?

A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member. A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.


In Python

Python doesn't have public OR private attributes. All attributes are accessible to all code. Private attributes are therefore (in Python) merely a convention. “Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member).


Suggested Video


Code examples

https://www.tutorialsteacher.com/python/public-private-protected-modifiers

https://stackoverflow.com/questions/4555932/public-or-private-attribute-in-python-what-is-the-best-way

https://medium.com/@cesarkohl/public-protected-and-private-properties-in-oop-de03166d23cf

www.teachyourselfpython.com