Puzzle 7 User! Identify Yourself
Python Brain Teasers — by Miki Tebeka (16 / 40)
👈 Spam, Spam, Spam | TOC | sorted? reversed? 👉
next_uid = 1
class User:
def __init__(self, name):
global next_uid
self.name = name
self.__id = next_uid
next_uid += 1
u = User('daffy')
print(f'name={u.name}, id={u.__id}')
Guess the Output
IMPORTANT
Try to guess what the output is before moving to the next page.
This code will raise an AttributeError exception.
Python does not have private and protected attributes like other languages (we joke that Python is a language for consenting adults).
By convention, if you prefix your attributes (or variables) with _ (called underscore), they are considered an implementation detail. You can still access them, but the author doesn’t consider them part of the public API and might rename or remove them in the next version.