I have a question about reverse().

Asked 2 years ago, Updated 2 years ago, 132 views

phone_number='01033334444'
phone_number=list(phone_number)
phone_number=phone_number.reverse()
print(phone_number)

I want to use reverse to solve problems in other places besides this None is output every time you run.

I want to know why None is printed.

python reverse

2022-09-20 21:45

1 Answers

>>> help(list.reverse)
Help on method_descriptor:

reverse(...)
    L.reverse() -- reverse *IN PLACE*

>>> l = list("abcde")
>>> l.reverse()
>>> l
['e', 'd', 'c', 'b', 'a']

Help provides a description of the reverse function. It is highlighted as IN PLACE. Instead of return the inverted list, as expected by other functions, the list itself is a method that is reversed within .

If you run a function in Python that normally has no return defined and save its return value, None is saved.

Do you understand?


2022-09-20 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.