How to get the concrete class name as a string?

This question already has an answer here:

  • Getting the class name of an instance in Python 8 answers

  •  instance.__class__.__name__
    

    例:

    >>> class A():
        pass
    >>> a = A()
    >>> a.__class__.__name__
    'A'
    

    <object>.__class__.__name__
    

    you can also create a dict with the classes themselves as keys, not necessarily the classnames

    typefunc={
        int:lambda x: x*2,
        str:lambda s:'(*(%s)*)'%s
    }
    
    def transform (param):
        print typefunc[type(param)](param)
    
    transform (1)
    >>> 2
    transform ("hi")
    >>> (*(hi)*)
    

    here typefunc is a dict that maps a function for each type. transform gets that function and applies it to the parameter.

    of course, it would be much better to use 'real' OOP

    链接地址: http://www.djcxy.com/p/40936.html

    上一篇: 获取没有模块的类名等

    下一篇: 如何将具体的类名称作为字符串?