总结下来:
1、reverse()是列表的内置方法,即:字典、元组、字符串没有这个方法,用于列表元素的翻转。
>>> lista=[1,2,3,4]
>>> lista.reverse()
>>> print(lista)
[4, 3, 2, 1]
lista.reverse() 这一步操作的返回值是一个None,其作用的结果,需要通过打印被作用的(即是哪个列表调用了这个reverse()方法)列表才可以查看出具体的效果。
2、reversed:而reversed()是python自带的一个方法,准确说,应该是一个类
reversed(sequence) -> reverse iterator over values of the sequence Return a reverse iterator
reverse(sequence) - >反转迭代器的序列值 返回反向迭代器
在经过reversed()的作用之后,返回的是一个把序列值经过反转之后的迭代器,所以,需要通过遍历,或者List,或者next()等方法,获取作用后的值
>1. 列表的反转:
>>> listb=[1,2,3,4]
>>> print(reversed(listb))
<list_reverseiterator object at 0x7f708474ec40>
>>> print(list(reversed(listb)))
[4, 3, 2, 1]
>2.元组的反转:
>>> tpl=(1,3,5,7)
>>> print(tuple(reversed(tpl)))
(7, 5, 3, 1)
>>> print(list(reversed(tpl)))
[7, 5, 3, 1]
>3. 字符串反转
>>> ss='12345abcde'
>>> print((reversed(ss)))
<reversed object at 0x7fc50e8abd90>
>>> print(''.join(reversed(ss))) #无分隔符
edcba54321
>>> print(','.join(reversed(ss)))#以逗号分隔
e,d,c,b,a,5,4,3,2,1
>>> print(' '.join(reversed(ss))) #以空格分隔
e d c b a 5 4 3 2 1
如何查看一个类或对象有哪些方法属性,参考这里
以reversed类为例:
[vagrant@vm-node1:~]$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> i=5500171714335001507730457227127633683517613019341760098818554179534751705629
>>> fmt=format(i,'b')
>>> print(fmt)
110000101000111111001010001110000110110001111010001000100111011000000000101100101111111001010000101101111100101011100001000111101100100001101101001110111111000111111011111001000111000110111110100010011000001001111110000110011101011100101010101000011101
>>> rev=reversed(fmt)
>>> print(list(rev))
['1', '0', '1', '1', '1', '0', '0', '0', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '0','1', '0', '1', '1', '1', '0', '0', '1', '1', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '1', '0', '0', '1', '0', '0', '0', '1', '0', '1', '1', '1', '1', '1', '0', '1', '1', '0', '0', '0', '1', '1', '1', '0', '0', '0', '1', '0', '0', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '0', '0', '0', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '0', '1', '0', '1', '1', '0', '1', '1', '0', '0', '0', '0', '1', '0', '0', '1', '1', '0', '1', '1', '1', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '1', '0', '1', '0', '1', '0','0', '1', '1', '1', '1', '1', '0', '1', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '0', '1', '1', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '1', '1', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '1', '1', '1', '0', '0', '0', '1', '1', '0', '1', '1', '0', '0', '0', '0', '1', '1', '1', '0', '0', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '1', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '1']
如果直接对int类型调用reversed,则会报错:
>>> print(reversed(i))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not reversible
>>> print(list(reversed(str(i))))
['9', '2', '6', '5', '0', '7', '1', '5', '7', '4', '3', '5', '9', '7', '1', '4', '5', '5', '8', '1', '8', '8', '9', '0','0', '6', '7', '1', '4', '3', '9', '1', '0', '3', '1', '6', '7', '1', '5', '3', '8', '6', '3', '3', '6', '7', '2', '1', '7', '2', '2', '7', '5', '4', '0', '3', '7', '7', '0', '5', '1', '0', '0', '5', '3', '3', '4', '1', '7', '1', '7', '1', '0', '0', '5', '5']
查看相应的Python对象是否包含需要的函数或常量。介绍如何查看Python对象中包含哪些属性,如成员函数、变量等,这里的Python对象指的是类、模块、实例等包含元素比较多的对象
>1.dir()函数
dir([object]) 会返回object所有有效的属性列表
>>> dir(rev)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__']
>>> dir(reversed)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__']
>>> reversed.__dict__ #__dict__:对类进行调用
mappingproxy({'__new__': <built-in method __new__ of type object at 0x55f641d0f200>, '__getattribute__': <slot wrapper '__getattribute__' of 'reversed' objects>, '__iter__': <slot wrapper '__iter__' of 'reversed' objects>, '__next__': <slot wrapper '__next__' of 'reversed' objects>, '__length_hint__': <method '__length_hint__' of 'reversed' objects>, '__reduce__': <method '__reduce__' of 'reversed' objects>, '__setstate__': <method '__setstate__' of 'reversed' objects>, '__doc__': 'Return a reverse iterator over the values of the given sequence.'})
>>> rev.__dict__ #rev没有__dict属性,调用__dict__会报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'reversed' object has no attribute '__dict__'. Did you mean: '__dir__'?
>2. vars() 函数
vars([object]) 返回object对象的__dict__属性,其中object对象可以是模块,类,实例,或任何其他有__dict__属性的对象。所以,其与直接访问__dict__属性等价。示例如下(这里是反例,rev对象中没有__dict__属性)
>>> vars(rev)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute
>>> vars(reversed)
mappingproxy({'__new__': <built-in method __new__ of type object at 0x55f641d0f200>, '__getattribute__': <slot wrapper '__getattribute__' of 'reversed' objects>, '__iter__': <slot wrapper '__iter__' of 'reversed' objects>, '__next__': <slot wrapper '__next__' of 'reversed' objects>, '__length_hint__': <method '__length_hint__' of 'reversed' objects>, '__reduce__': <method '__reduce__' of 'reversed' objects>, '__setstate__': <method '__setstate__' of 'reversed' objects>, '__doc__': 'Return a reverse iterator over the values of the given sequence.'})
>3. help() 函数
help([object])调用内置帮助系统。输入
>>> help(rev)
Help on reversed object:
class reversed(object)
| reversed(sequence, /)
|
| Return a reverse iterator over the values of the given sequence.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
:
>>> help(reversed)
Help on class reversed in module builtins:
class reversed(object)
| reversed(sequence, /)
|
| Return a reverse iterator over the values of the given sequence.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
:
>4. type() 函数
type(object)返回对象object的类型
>>> type(rev)
<class 'reversed'>
>>> type(reversed)
<class 'type'>
>5. hasattr() 函数
hasattr(object, name)用来判断name(字符串类型)是否是object对象的属性,若是返回True,否则,返回False
>>> hasattr(rev,'__dict__')
False
>>> hasattr(reversed,'__dict__')
True
>6. callable() 函数
callable(object):若object对象是可调用的,则返回True,否则返回False。注意,即使返回True也可能调用失败,但返回False调用一定失败。
>>> callable(rev)
False
>>> callable(reversed)
True