Is there a way to define a list (obj) method for a custom class in python?

You can implement the __str__(self) method to return a string. Is there an analogue of __list__(self) ? ( __list__ does not work!) Is there a .toList () path?

+5
source share
1 answer

There is no direct analogue, but list() is implemented by iterating over the argument. So use iterator protocol ( __iter__() ) or related indexing ( __len__() and __getitem__() ).

+15
source

All Articles