[Tutor] Levels of Politeness, or Granularity and Placement of Methods?

Lloyd Kvam pythontutor@venix.com
Fri, 07 Jun 2002 09:22:20 -0400


I would recommend using setattr(self, attr, value) rather than
self.__dict__[attr] = value

setattr() uses the regular __getattr__ and __setattr__ functions and will
therefore use any special handling that you code for your class.  Direct
assignment to the dictionary is useful when you MUST avoid the normal handling,
but I would not use it otherwise.

My rules of thumb are that classes should be as ignorant of each other as
possible.  Typically, a class knows the names of methods in the other classes
that it relies on.  When a method would simply be getProperty, I normally just
access the property directly (e.g. self.prop = obj.prop).  In Python, this is
very reasonable because the obj.__getattr__ can easily provide any special
handling that a getProperty method would have implemented.  (Also there are
new features in Python 2.2 for property handling.)

In your case, Card.effects is a dictionary.  (self.__dict__[effect] = effects[effect])
The Player knows that Card.effects is a dictionary.  The Player applies effects
by assigning the effects dictionary to self.  This is more class knowledge than
I like.

My inclination would be to either have:
	card.applyEffects( obj) where obj would be a player, but could be any object
or
	create an Effects class that implements effect.apply( obj) as in Card.

The apply method might return a list of changed attributes.
	#in Player
	self.applied_cards.append(deck.pop(card))
	changes = card.applyEffects( self)
	for change in changes:
		__player responds to change from effects__

NOTE, this means you need to watch out for attribute name conflicts!  If this is a real
concern use an Effects Class to package the effect names.  The player.effects are updated
from/by the card.effects depending on which seems to fit.
	card.effects.applyTo( player.effects) versus player.effects.updateFrom( card.effects)



HTH

Israel Evans wrote:

>  
> 
> I have a question for the Tutor Community that pertains to OOP and, um 
> other stuff like that.  J
> 
> 
> ~Israel~

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582