Defining magic methods on classes

我的梦境 提交于 2019-12-10 16:26:59

问题


I would like to define a single object that can be iterated over without having to create a class and then an instance. Something like this:

class Thing(object):
    stuff = ["foo", "bar", "baz"]

    @classmethod
    def __iter__(cls):
        return iter(cls.stuff)

for thing in Thing:
    print thing

However this doesn't actually work. Is there any way to do this?


回答1:


What Ashwini correctly suggested in his comment is the following. This works in Python 2.

class ThingType(type):
    __stuff__ = ["foo", "bar", "baz"]

    @classmethod
    def __iter__(cls):
        return iter(cls.__stuff__)

class Thing(object):
    __metaclass__ = ThingType

for thing in Thing:
    print thing

And this works in Python 3:

class ThingType(type):
    __stuff__ = ["foo", "bar", "baz"]

    @classmethod
    def __iter__(cls):
        return iter(cls.__stuff__)

class Thing(object, metaclass=ThingType):
    pass

for thing in Thing:
    print(thing)



回答2:


Does Thing actually need to be a type? You could make it an object that has type-like callable behavior, which would probably be simpler:

class RealThing(object):
  pass

class ThingFactory(object):
  def __iter__(self):
    return iter(["foo", "bar", "baz"])

  def __call__(self):
    return RealThing()

Thing = ThingFactory()


来源:https://stackoverflow.com/questions/29759273/defining-magic-methods-on-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!