开发者

Python __str__: Magic Console

Suppose one dec开发者_运维知识库ided (yes, this is horrible) to create handle input in the following manner: A user types in a command on the python console after importing your class, the command is actually a class name, the class name's __str__ function is actually a function with side effects (e.g. the command is "north" and the function changes some global variables and then returns text describing your current location). Obviously this is a stupid thing to do, but how would you do it (if possible)?

Note that the basic question is how to define the __str__ method for a class without creating an instance of the class, otherwise it would be simple (but still just as crazy:

class ff:
    def __str__(self):
        #do fun side effects
        return "fun text string"

ginst = ff()

>>ginst


What you are looking for is the metaclass

class Magic(type):
    def __str__(self):
        return 'Something crazy'
    def __repr__(self):
        return 'Another craziness'

class Foo(object):
    __metaclass__ = Magic

>>> print Foo
Something crazy
>>> Foo
Another craziness


in console you're getting representation of your object, which __repr__ is responsible for. __str__ used for printing:

>>> class A:
    def __str__(self):
        return 'spam'


>>> A()
<__main__.A object at 0x0107E3D0>
>>> print(A())
spam

>>> class B:
    def __repr__(self):
        return 'ham'


>>> B()
ham
>>> print(B())
ham

>>> class C:
    def __str__(self):
        return 'spam'
    def __repr__(self):
        return 'ham'


>>> C()
ham
>>> print(C())
spam


You could use instances of a class rather than classes themselves. Something like

class MagicConsole(object):
    def __init__(self, f):
        self.__f = f

    def __repr__(self):
        return self.__f()

north = MagicConsole(some_function_for_north)
south = MagicConsole(some_function_for_south)
# etc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜