Passing objects to a function

Robert Brewer fumanchu at amor.org
Sun May 9 19:36:53 EDT 2004


Thomas Philips wrote:
> I'd like to write the parameter list of a method or function in a way
> that makes the class of each parameter crystal clear, thus decreasing
> the likelihood of a programming error.
> 
> I am programming a blackjack game with Card and Hand objects, and
> these in turn have methods that require Card objects, Hand objects or
> both. For example, the class Hand has a method BlackJackValue that
> computes the value of a hand using the rules of blackjack. I'd like to
> write the method (approximately) in the form
> 
> def BlackJackValue(hand=Hand):
>    adfsadf
>    adfasdf
>    .
>    .
>    .
> 
> It takes only one look at this method's definition to realize that it
> requires an object of type Hand. Unfortunately, my mangled syntax
> describes the way in which default parameters are passed, and does not
> allow me to document the class of each parameter. What's the right way
> to achieve my goal?

Since it's a bound method, the "right" way for the above would be to use
"self". ;)

Barring that, however, the right way to document a function is: the
documentation string, of course!

>>> str.join.__doc__
'S.join(sequence) -> string\n\nReturn a string which is the
concatenation of the strings in the\nsequence.  The separator between
elements is S.'

...following the example for the 'str' builtin, you might write:

def BlackJackValue(hand):
    """BlackJackValue(Hand_instance) -> int\n\nReturn an integer, which
is the value of the Hand according to the rules of BlackJack."""
    pass



Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list