Namespace weirdness

Cliff Crawford cjc26 at nospam.cornell.edu
Mon Jul 10 20:35:14 EDT 2000


* Paul Winkler <slinkp23 at yahoo.com> menulis:
| 
| But I overlooked the obvious. Just stuff all those arguments into a
| dictionary!
| Then my code above looks like:
| 
| class Food:
| 	def __init__(self, args = {}):

You could do something even trickier here:

        def __init__(self, **args):

This allows you to call __init__ with keyword arguments, like this:

        f = Food(color='red', texture='slimey', flavor='salty')

args will be passed a dictionary of all the keyword arguments, so this
is basically like the solution you came up with, only with a possibly
nicer syntax :)


| 		self.args = args
| 		self.thing1 = SomeClass(self.args)
| 		self.thing2 = SomeClass(self.args)
| 		...
| 
| class SomeClass:
| 	def __init__(self, args)

If you choose to use keyword arguments above, you probably should leave
SomeClass as it is, because it's not too easy to pass a dictionary to a
function expecting keyword arguments.  It >can< be done, though, using
the apply function.


| 		self.args = args
| 	def do_something_useful:
| 		print self.args['some_key']
| 		...
| 
| Ahhh, much better. Now I only have to deal with the entire list of
| args (now keys) once, when I create a Food instance, and then just
| reference each by key when needed.
| 
| And accessing the parameters as args['key'] is actually a lot easier
| than typing self._parentInstance.arg1 or some such weirdness.
| 
| 
| p.s. there are several common English insults that take the form of
| Food.BodyPart, hence my weird choice of names.
| 
| egg.head
| meat.head
| cheese.head
| lard.ass
| pizza.face
| beer.belly
| 
| I have the feeling there are more...

let's-all-hear-it-for-synecdoche-ly y'rs, Cliff


-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
                          Synaesthesia now!            icq 68165166



More information about the Python-list mailing list