Namespace weirdness

Paul Winkler slinkp23 at yahoo.com
Mon Jul 10 06:43:04 EDT 2000


Tim Hochberg wrote:
> Just to add to keep tings a bit confusing, let me show a way to do more or
> less what you're trying to do. I don't really recomend it, but it's an
> entertaining little trick. There are two signifigant changes: first the
> class definition is moved up yet another level so that it's defined in the
> __init__ function. This is because you're really trying to get at instance
> data, not class data. The second change is to specificall add the instance
> namespace to the BodyPart class. I also manually added BodyPart to the
> instance namespace since otherwise there would be no way to get at it. So
> here, for your entertainment is the final version. Enjoy!
> 
> class Food:
>    def __init__(self, attr1):
>        self.attr1 = attr1
>        class BodyPart:
>            def do_it(self):
>                print self._foodInstance.attr1
>        BodyPart._foodInstance = self
>        self.BodyPart = BodyPart

That is very peculiar indeed. attr1 is now shared across all
instances of Food.BodyPart() which indeed has the effect I wanted.
I can even manually change attr1 and it affects all subsequent calls
to Food.BodyPart.do_it().

I like that, but the fact that it's so unusual makes me think that
this is probably not the best way to design my app...

The whole thing I was trying to avoid was passing a hundred
arguments to a dozen objects contained within a Food instance, and
it turns out there's a better way. (I should learn to just ask "How
should I do x?" rather than "Can I do x in this particular strange
fashion?")

The job I'm trying to do has a _lot_ of variables involved, and
being a novice I of course came up with the weirdest possible
strategy for avoiding a lot of typing. :) And of course there's a
better way, see below.

For instance I didn't want to have to say:

class Food:
	def __init__(self, arg1, arg2, arg3, ...arg100):
		self.arg1 = arg1
		self.arg2 = arg2
		...
		self.arg100 = arg100
		# Note below that I can't just say SomeClass(arg1, arg2...)
		# because then changing some_food_instance.arg1 will not
		# affect some_food_instance.some_SomeClass_instance,
		# which I need it to do.
		self.thing1 = SomeClass(self.arg1, self.arg2, ...self.arg100)
		self.thing2 = SomeClass(self.arg1, self.arg2, ...self.arg100)
		...

class SomeClass:
	def __init__(self, arg1, arg2, arg3... arg100)
		self.arg1 = arg1
		self.arg2 = arg2
		self.arg3 = arg3
		...
		self.arg100 = arg100

...

See my point? for each argument, I have to type the argument twice
to get it into my Food class, then twice more for every class that
needs to do something with it, then at least once more to actually
use it in a method.
So for N parameters that affect Food, I have to type as many as N *
5 variables just to do anything at all. This seems like too much
work. If N = 100, and it might be, it will drive me crazy.

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 = {}):
		self.args = args
		self.thing1 = SomeClass(self.args)
		self.thing2 = SomeClass(self.args)
		...

class SomeClass:
	def __init__(self, args)
		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...
-- 
.................    paul winkler    ..................
slinkP arts:   music, sound, illustration, design, etc.
        web page:  http://www.ulster.net/~abigoo        
      A member of ARMS:   http://www.reacharms.com
               or http://www.mp3.com/arms



More information about the Python-list mailing list