[Edu-sig] Re: A better way?

Kirby Urner urnerk at qwest.net
Mon Nov 8 02:05:25 CET 2004


Scott:

> What's wrong with:
> 
>  >>> class Test:
>   	 def __init__(self):
> 	     self.attr1 = 1
> 	     self.attr2 = 2
> 	 def method1(self): print "Cough"
> 	 def method2(self): print "Chuckle"
> 	 def __getitem__(self, value):
> 	     return getattr(self, value)
> 

Yeah, that's better.  Something similar came to me in the shower just now.

 >>> class Test(object): 
 	  def __init__(self):
		self.attr1 = 1
		self.attr2 = 2
	  def method1(self): print "Cough"
	  def method2(self): print "Chuckle"
	  def __getitem__(self, value):
		return self.__getattribute__(value)

	
 >>> otest = Test()
 >>> otest['attr1']
 1
 >>> otest['attr2']
 2
 >>> otest['method1']()
 Cough
 >>> otest['method2']()
 Chuckle

But I like yours better.  Thanks.

Kirby




More information about the Edu-sig mailing list