KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
The Data Model section of the Python 3.2 documentation provides the following descriptions for the __int__ and __index__ methods: object.__int__(self) Called to implement the built-in [function int() ]. Should return [an integer]. object.__index__(self) Called to implement operator.index() . Also called whenever Python needs an integer object (such as in slicing, or in the built-in bin() , hex() and oct() functions). Must return an integer. I understand that they're used for different purposes, but I've been unable to figure out why two different methods are necessary. What is the difference between these methods? Is it safe to just alias __index__ = __int__ in my classes?
Tags (comma-separated)
Save Edits
Cancel