Skip to main content

Magic Methods, Properties, and Iterators

  • Chapter
  • First Online:
  • 20k Accesses

Abstract

In Python, some names are spelled in a peculiar manner, with two leading and two trailing underscores. You have already encountered some of these (_ _future_ _, for example). This spelling signals that the name has a special significance—you should never invent such names for your own programs. One very prominent set of such names in the language consists of the magic (or special) method names. If your object implements one of these methods, that method will be called under specific circumstances (exactly which will depend on the name) by Python. There is rarely any need to call these methods directly.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   69.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Notes

  1. 1.

    Thanks to Alex Martelli for pointing out this idiom and the importance of using it here.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2017 Magnus Lie Hetland

About this chapter

Cite this chapter

Hetland, M.L. (2017). Magic Methods, Properties, and Iterators. In: Beginning Python. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-0028-5_9

Download citation

Publish with us

Policies and ethics