关于python工厂函数

str()函数就是工厂函数,它返回字符串对象,都是像工厂一样标准化处理过的
同理,int()、list()、tuple()等等这些,全部都是工厂函数
工厂函数不是函数
工厂函数实际不是函数,而是一个类对象,返回都是类都实例
str源码,它是一个类

class str(object):
    """
    str(object='') -> str
    str(bytes_or_buffer[, encoding[, errors]]) -> str
    
    Create a new string object from the given object. If encoding or
    errors is specified, then the object must expose a data buffer
    that will be decoded using the given encoding and error handler.
    Otherwise, returns the result of object.__str__() (if defined)
    or repr(object).
    encoding defaults to sys.getdefaultencoding().
    errors defaults to 'strict'.
    """
    def capitalize(self, *args, **kwargs): # real signature unknown
发布了7 篇原创文章 · 获赞 0 · 访问量 860

猜你喜欢

转载自blog.csdn.net/qq_45399019/article/details/100053724