0

Check out this code in my interpreter:

$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
<<< first_list = [1,2,3,4,5,6]
<<< second_list = [101,202,303,404]
<<< first_list.reverse()
<<< print(first_list)
[6, 5, 4, 3, 2, 1]
<<< reverse(second_list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'reverse' is not defined
<<<

In the first line and second lines, I define two lists. Then I proceed to reverse the order of the first_list and second_list in two different ways. The first reversal succeeds whereas the second way is rejected.

Putting the variable inside the reverse function as a parameter is how I initially would naturally use it if I were to write a script (which evidently would be rejected by the Python interpreter).

The instructor in the Udemy course I am taking suggests putting the variable in front of the function separated by a dot. I understand that the computer dictates what works and what doesn’t. I just want to know why since my (faulty) approach comes so much more naturally.

How come variables sometimes have to go in front of the function when other times it can go inside a function as a parameter?

I suppose the much more important question I now have is this: When I am manipulating variables, how do I know when to put variables outside vs inside?

Thanks for your attention.

enoren5
  • 403
  • 5
  • 18
  • 1
    This question might help https://stackoverflow.com/q/20981789/831878 – Ray Toal Jun 02 '18 at 03:58
  • Do you want `list(reversed(second_list))`? – Austin Jun 02 '18 at 04:02
  • 1
    `reversed` is the built in function that takes an argument. the `reverse` method of the list object takes no arguments and reverses the list its bound to. You just need to use `reversed(second_list)` for that last case – avigil Jun 02 '18 at 04:13

2 Answers2

2

This occurs because the reverse() function is created as an attribute of the first_list object. Thus, reverse() is not a function you can call and provide first_list as an argument.

It's important to know that everything in Python is an object, and every object has attributes.

For example, if I create a list in the python interpreter, new_list = [1, 2, 3], and then do dir(new_list) (this shows the attributes of new_list), I get the following:

>>> new_list = [1, 2, 3]
>>> dir(new_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__delslice__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', 
'__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', 
'__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', 
'__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 
'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

And therein lies the reverse function for the object list.

rst-2cv
  • 1,130
  • 1
  • 15
  • 31
1

reverse is a method of the list data type, python is looking for a definition of reverse when you call it without the dot notation. If you had in your source code somewhere def reverse()... it would use that, or if you had imported a library that defined reverse. The first list works because first_list is a object that has a method called reverse.

python3 documentation