+ 7

Why we use this in python?

1. x=[1,3,2,4] 2. x.sort() 3. print(x),can you tell me why we use parentheses after write sort in line 2,why we use these parentheses to write many methods.

28th May 2018, 4:52 PM
Maninder $ingh
Maninder $ingh - avatar
3 ответов
+ 6
Because attributes are a thing. For example: class Car: def __init__(self, color): self.color = color car = Car("red") car.color # this value is "red" If methods were without parentheses, it would be hard to distinguish between an attribute and a method (car.color and car.color(), for example). Some languages do have parenthesesless methods, like Ruby, but I find that less readable. Even so, you can use parentheses if you want.
28th May 2018, 4:59 PM
LunarCoffee
LunarCoffee - avatar
+ 4
We use parentheses for methods to differentiate them from variables.
28th May 2018, 4:57 PM
Vlad Serbu
Vlad Serbu - avatar
0
Hello Maninder As Say Vlad sort() is a method or function. We use sort() in these langages:javascript,php.. it displays your result in order(alphabetical too) sort() is different than reverse() Bye
29th May 2018, 6:31 PM
soules