You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type('')
<class 'str'>
>>> type('').startswith('s')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: startswith() takes at least 1 argument (0 given)
>>> type('').startswith('s', 's')
True
This is exactly how methods on Python object have been behaving for year: any method can be called either as method on instance, or as method on class, with instance passed as the first argument.
To explain in more detail: type('s').startswith is the same as str.startswith, which is an unbound method in Python 2 and a regular function in Python 3. Either way, it expects two arguments: a string which becomes "self", and a second string argument, which is the prefix being tested for.
So type('any string').startswith('alphabet', 'al') is a long way of writing 'alphabet'.startswith('al').
Activity
VertigoRay commentedon May 14, 2016
This doesn't look like proper functionality
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> type('') <class 'str'> >>> type('').startswith('s') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: startswith() takes at least 1 argument (0 given) >>> type('').startswith('s', 's') TrueSilentGhost commentedon May 14, 2016
This is exactly how methods on Python object have been behaving for year: any method can be called either as method on instance, or as method on class, with instance passed as the first argument.
stevendaprano commentedon May 14, 2016
To explain in more detail:
type('s').startswithis the same asstr.startswith, which is an unbound method in Python 2 and a regular function in Python 3. Either way, it expects two arguments: a string which becomes "self", and a second string argument, which is the prefix being tested for.So type('any string').startswith('alphabet', 'al') is a long way of writing 'alphabet'.startswith('al').