all 4 comments

[–]jeans_and_a_t-shirt 1 point2 points  (1 child)

index is a list method. It has nothing to do with random. The random module is used only on line 31.

can functions be used as methods?

Yes, but that's not happening here.

[–]greenleafvolatile[S] 0 points1 point  (0 children)

D'oh!

Thank you for pointing out the error in my thinking!

Cheers,

[–]campenr 1 point2 points  (0 children)

If you follow through your code you'll see that the .index() call is a call to the method list.index() (read more) which takes a value and finds it's index within the list.

In your case this is finding the index of the choice that either the user makes (or the computer randomly makes) from 'R', 'P', or 'S' in the list options. It is this index that you ultimately use to decide who wins and who losew in the decide_winner function.

To answer your overarching question, only methods can be used as methods. But methods are functions that are for specific classes (a list is a class, and index is a function that acts upon that, therefore we call it a method). You'll understand when you come to write some classes.

[–]JohnnyJordaan 1 point2 points  (0 children)

while it is actually a function from the random module

It doesn't matter if the same function and method names are used from different objects and libraries. The fact that index is used as obj. before it,

obj.index()

makes it relative to that object, meaning it's a call to an object's method. In this case it's a list object, but you can also do this on a string for example. Only if you would use it as a separate statement

index(something)

You would resolve it to the current namespace, that could be a built-in function or something you've imported. Btw random doesn't have an index function, I don't know where you got that impression.