[Python-de] Re: dynamische variablen generieren

Johannes Krauth Johannes.Krauth at partner.bmw.de
Die Sep 28 12:42:08 CEST 2004


hallo,

danke die ist wirklich k(c)lasse!

gruss

josh

> >>> class VarInstance(object):
> ...     def __init__(self, dictWithValues):
> ...             self.__dict = dictWithValues
> ...     def __getattr__(self, wants):
> ...             if self.__dict.has_key(wants):
> ...                     return self.__dict[wants]
> ...             else:
> ...                     raise NameError
> ...
> >>> dictWithValues = {'var1':'Hi', 'var2':3, 'var4':45}
> >>> myVars = VarInstance(dictWithValues)
> >>> myVars.var1
> 'Hi'
> >>> myVars.var2
> 3
> >>> myVars.var3
> Traceback (most recent call last):
>    File "<interactive input>", line 1, in ?
>    File "<interactive input>", line 8, in __getattr__
> NameError
> >>> myVars.var4
> 45
> >>>
> 
> Mit vielen Gruss-Instanzen,
> Marco
> 
>   ------------------------------------------------------------------------
> _______________________________________________
> python-de maillist  -  python-de at python.net
> http://python.net/mailman/listinfo/python-de

-- 

Johannes Krauth

---------------

BMW-Group EA-92 01.51/78.5-0 EG
Tel. +49-89-382-58636
Fax. +49-89-382-31830
mailto:Johannes.Krauth at partner.bmw.de

---------------

CENIS GmbH