python 对象

来源:互联网 发布:前端连接数据库 编辑:程序博客网 时间:2024/06/06 10:51

目标,能够查看python对象属性和文档

python自带的对象很多

>>> int#整数<class 'int'>>>> str#字符串<class 'str'>>>> float#浮点数<class 'float'>>>> 

字符串的赋值

>>> c = "hello">>> b = 'hello'>>> d = """hello""">>> e = '''hello'''>>> c==bTrue>>> b==dTrue>>> e==cTrue>>> type(c)<class 'str'>>>> 

type 可以查看对象类型.
上述赋值方法都可以用于字符串赋值,按照python简洁的风格,建议使用b='hello'的形式.

字符串的属性

>>> dir(c)['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']>>> 

dir可以查看拥有什么属性
则这属性的描述可以这样查看

>>> c.split.__doc__'S.split(sep=None, maxsplit=-1) -> list of strings\n\nReturn a list of the words in S, using sep as the\ndelimiter string.  If maxsplit is given, at most maxsplit\nsplits are done. If sep is not specified or is None, any\nwhitespace string is a separator and empty strings are\nremoved from the result.'

__doc__这个一般的对象都拥有的,也就是帮助文档,建议以后写函数的时候尽量写该属性.

python还有其他自带的很多属性和方法,可以按照以上的方法学习和使用.