assigning values in __init__

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Nov 9 10:37:53 CET 2006


On Thu, 09 Nov 2006 12:27:12 +1100, Ben Finney wrote:

> John Salerno <johnjsal at NOSPAMgmail.com> writes:
> 
>> Ben Finney wrote:
>> > If you pass a *mapping* of the
>> > "I-might-want-to-add-more-in-the-future" values, then you get both
>> > explicit *and* expandable, without an arbitrary unneeded sequence.
>>
>> Do you mean by using the **kwargs parameter?
> 
> No. 

Well, that'll teach me to put words in your mouth.

[snip]
> If you have a group of named, semantically-related, unsequenced values,
> pass them into the function as a mapping object (a dict object).

Still, if you are doing this:

mapping_object = {"strength": roll_dice(10),
    "intelligence":roll_dice(10),
    "dexterity":roll_dice(10)}
my_character = Character(mapping_object)

then there is little benefit to building the dict just for the purposes of
passing it to Character(), never to use it again, not when you can do this:

my_character = Character(strength: roll_dice(10), 
    intelligence:roll_dice(10), dexterity:roll_dice(10))

If you happen to already have collected your character attributes in a
mapping object for some other reason, then well and good, pass it into the
function. Otherwise, well, I believe the correct container for
character attributes is a Character, not a dict.

-- 
Steven.




More information about the Python-list mailing list