Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries Dictionary Iteration

Dict Methods: Nomenclature

When Kenneth accessed the list of methods that a dict could use, a lot of the methods had two "_ _" around it. What does that mean?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

According to PEP-8: __double_leading_and_trailing_underscore__ are "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__ , __import__ or __file__. Never invent such names; only use them as documented.

When you run the command dir() on an object to list the object attributes , behind the scenes Python is actually calling the object.__dir__() method. Many actions in Python get redirected to "dunder" methods. Another example is len(object) actually calls the object.__len__() method. While PEP-8 advises not to create your own dunder methods, you can (and often will) overwrite the dunder methods of a parent class (This will get covered later when the Python track covers classes)..