Why can I say this:
print (max('abcdefg'))
print (sorted('akjsdhfjkh'))
but not this:
print(capitalize(cat)
and rather this:
v='cat'
print (v.capitalize())
Why can I say this:
print (max('abcdefg'))
print (sorted('akjsdhfjkh'))
but not this:
print(capitalize(cat)
and rather this:
v='cat'
print (v.capitalize())
Short answer:
Nothing profound--just the calling conventions that were chosen for these functions.
Slightly longer answer:
max() and sorted() are functions that operate on sequences--you give them a sequence (list, tuple, or string) and they return a new one.
capitalize() is a method for str objects. As with many of these, there is also a str package method that takes an argument. So you can call it like this if you prefer: str.capitalize(v)
max
, sorted
are regular builtin functions
In [30]: max
Out[30]: <function max>
Where capitalize
is a method of str
class
In [31]: str.capitalize
Out[31]: <method 'capitalize' of 'str' objects>
In [32]: str.capitalize("aaa")
Out[32]: 'Aaa'
In [33]: "aaa".capitalize()
Out[33]: 'Aaa
capitalize is a method of the string object: https://docs.python.org/2/library/string.html#string.capitalize
sorted and max are built-in functions: https://docs.python.org/2/library/functions.html
There is no logic here.
You were perfectly correct that any sort of iterable data structure (such as a string or a list) should have a built-in max function .
i.e.
'abcdefg'.max()
The creators of Python had chosen a small number of operations that they want to be considered as built-in: https://docs.python.org/2/library/functions.html
Most other functions are part of specific classes or objects.
I think its perfectly reasonable for max to not have been a built-in and instead been part of some math library
i.e.
import math
math.max('abcdefg')
look for difference between the method and function. Major difference is that we can operate function independently while in case of method we have to use it with certain object.
You can do 'cat'.capitalize()
by the way.
In Python, there are functions and there are methods. A function can be called on its own. A method needs to be called from an object of its class.
Imagine you have this python file:
def my_print1(): # this is a function
print("cats are nice")
class test():
def my_print2(): # this is a method
print("dogs are nice")
To call the my_print1 function you just do my_print1()
and it will print that cats are nice.
But to call the my_print2 method you need to have an object of class test
and then that object can call my_print2 method. Here's how you can do that in code:
myObject = test() # this creates an object of class test
myObject.my_print2() # this calls the method using that object
You see how my_print2 was called using an object? This is just like doing v.capitalize()
. v
here is an object of the built-in str
(string) class, and capitalize
is a method of that class, and so it needed an object (v) to be called from.
max
and sorted
are built-in function of python. They can called without needing an object.
Here are the built-in functions of python.