Passing objects to a function

Holger Türk htx1 at gmx.de
Mon May 10 02:24:41 EDT 2004



Robert Brewer wrote:
> Thomas Philips wrote:
>>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
> 

This could be supported by an assert statement:

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

     assert isinstance (hand, Hand)

     [...]

The method will throw an AssertionError if hand is not
an instance of Hand. Assertions can be turned off later for
performance reasons; with -O I guess.

Greetings,

Holger




More information about the Python-list mailing list