[Tutor] Do _all_ Python builtin methods/functions return "None" IF ...
Cameron Simpson
cs at cskk.id.au
Sat Jan 27 05:54:39 EST 2018
On 27Jan2018 09:18, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>On 27/01/18 02:56, boB Stepp wrote:
>> So my actual question is: For these types of methods/functions, is
>> Python Both versions 2 and 3) consistent throughout and *always*
>> returns None?
>
>Mostly, but only for mutable objects.
>So the string methods return a new string with the modifications
>because the original string is immutable and cannot be changed.
>
>So superficially, list handling methods return none on
>modification but string methods return the modified string.
>You need to bear in mind whether the object is mutable or
>not to understand the expected result.
For a big more context, the general rule is: if the method fetches a value from
the object eg its length, of course it returns something other than None. But
if a method _modifies_ the object (append an element to a list, close a file),
just affecting its internal state, then the method will usually return None.
These modification methods _could_ return a value, but the general practice is
not to.
Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)
More information about the Tutor
mailing list