[Tutor] .get dicionary question (mutability??)

Dave Angel d at davea.name
Fri Jun 29 18:09:58 CEST 2012


On 06/29/2012 11:32 AM, Emile van Sebille wrote:
> On 6/29/2012 6:45 AM Albert-Jan Roskam said...
>> Hi Steven,
>> Thanks for helping me. It makes more sense now. Calling append() on the
>> datatype list returns None but calling __add__ on the datatype int
>> returns, well, the result.
>
> I don't know that it's consistent across all types, but I've found
> that mutable types tend to change in place and return None while
> immutable types tend to return the altered result, which is as you
> describe below.
>
> Emile
>
>

Close.  methods/functions that modify their objects/arguments return
None, while those that do not modify, will return the result.

For example, take sort() and sorted().  The first method modifies its
object and returns None.  (It cannot be used on an immutable type)
The sorted() function does not modify its argument, and returns the
sorted item.  (It can be used on mutable or immutable types)

-- 

DaveA



More information about the Tutor mailing list