assigning values in __init__
Antoon Pardon
apardon at forel.vub.ac.be
Thu Nov 9 05:36:07 EST 2006
On 2006-11-09, Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> wrote:
> 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))
But you will have to adapt this if you want extra or different
characteristics.
Personnally I would prefer something like:
chardict = {}
for char in characteristics:
chardict[char] = roll_dice(10)
my_character = Character(chardict)
This way you only have to keep your characteristics in one place.
> 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.
What is wrong with keeping the character attributes in a dict in the
Character?
--
Antoon Pardon
More information about the Python-list
mailing list