-2

How do you override list(object) where object is an object defined by a class. Can you do something like

class foo:
    def __list__(self):
        return [None] # In my real code I want it to return a object

It would also be helpful if someone found a question with the answer I'm looking for.

Raymond
  • 396
  • 4
  • 21

1 Answers1

1

You can define __iter__ method to make your object iterable. list can then create results based on the resulting iterator. You can also use yield generator syntax:

class Foo:
    def __iter__(self):
        yield None

print(list(Foo())) # prints [None]
zch
  • 14,931
  • 2
  • 41
  • 49