Why doesn't this work? :)

Jeremy Moles jeremy at emperorlinux.com
Fri Oct 28 21:28:36 CEST 2005


On Fri, 2005-10-28 at 14:50 -0400, Chris Lambacher wrote:
> I think what you really want is:
> 
> try:
> 	# this will fail and be caught
> 	# below, weeee
> 	import foobar
> 
> except ImportError, error:
> 	class foobarclass:
> 		def __getattr__(*args, **kargs):
> 			return None
>     foobar = foobarclass()
> 
> print foobar.bg
>
> foobar in your version is a class.  By making it an instance, the __getattr__ method is properly called.
> 
> 
> -Chris

:)

Well, that's what I am using. :) What I'm wondering is if the other
method could work, of if it simply impossible in Python considering it's
underlying implementation.

> On Fri, Oct 28, 2005 at 02:02:29PM -0400, Jeremy Moles wrote:
> > Jumping right into the code (which should speak for itself):
> > 
> > # -----------------------------------
> > 
> > try:
> > 	# this will fail and be caught
> > 	# below, weeee
> > 	import foobar
> > 
> > except ImportError, error:
> > 	class foobar:
> > 		@staticmethod
> > 		def __getattr__(*args, **kargs):
> > 			return None
> > 
> > print foobar.bg
> > 
> > # -----------------------------------
> > 
> > This doesn't work and I'm just curious as to why? I can, of course, make
> > __getattr__ non-static, instantiate a foolbar object, and everything
> > works; but, the "idea" above seems cleaner and whatnot. :)
> > 
> > Am I misunderstanding something fundamental about the builtin __*
> > functions? Can they not be "static?"
> > 
> > No rush on this, just curious. I'm using the following in a more general
> > way, and it works fine for now... :)
> > 
> > # -----------------------------------
> > 
> > try:
> > 	import foobar
> > 
> > except ImportError, error:
> > 	class Foobar:
> > 		def __getattr__(*args, **kargs):
> > 			return None
> > 
> > 	foobar = Foobar()
> > 
> > print foobar.bg
> > 
> > # -----------------------------------
> > 
> > 
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list