Puzzle 7 User! Identify Yourself

Python Brain Teasers — by Miki Tebeka (16 / 40)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Spam, Spam, Spam | TOC | sorted? reversed? 👉

user.py

​ 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.

images/hline.png

This code will raise an AttributeError exception.

images/hline.png

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.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.