[python-madrid] Problema con __getattr__

Andrey Antoukh andsux at gmail.com
Thu Apr 12 10:30:55 CEST 2012


Solución a eso: no uses twisted \o/ ( ahí solté mi primer troll del día )

El 12 de abril de 2012 10:26, Manuel de la Peña <etil15 at gmail.com> escribió:

>
> On Apr 12, 2012 10:17 AM, "Vicente Ruiz" <vruiz2.0 at gmail.com> wrote:
> >
> > Yo seguía con el ejemplo de Miguel Angel en la cabeza :D
> >
>
> Es que yo como Andrei, nada de old class, menos en twisted :(
>
> > El 12 de abril de 2012 10:10, Manuel de la Peña <etil15 at gmail.com>
> escribió:
> >
> >> Estaba asumiendo que se extendida de object como mínimo :)
> >>
> >> On Apr 12, 2012 9:30 AM, "Vicente Ruiz" <vruiz2.0 at gmail.com> wrote:
> >>>
> >>> Coincido con Andrei :)
> >>>
> >>> En cuanto a lo que comentas, Manuel, no puedes llamar al padre usando
> old-style, la que estarías mezclando objetos y tipos. Para mantener la
> interfaz de getattr funciona así:
> >>>
> >>> class EasyDict:
> >>>     def __init__(self):
> >>>         self._values = {}
> >>>
> >>>     def add(self, k, v):
> >>>         self._values[k] = v
> >>>
> >>>     def __getattr__(self, name):
> >>>         if name in self._values:
> >>>             return self._values[name]
> >>>         raise AttributeError
> >>>
> >>>     def __str__(self):
> >>>         return str(self._values)
> >>>
> >>> y creo que te referías a este otro cuando hablas de llamar al padre:
> >>>
> >>> class EasyDict(dict):
> >>>     def add(self, k, v):
> >>>         self[k] = v
> >>>
> >>>     def __getattr__(self, name):
> >>>         try:
> >>>             return super(EasyDict, self).__geattr__(name)
> >>>         except AttributeError:
> >>>             if name in self:
> >>>                 return self[name]
> >>>             raise AttributeError
> >>>
> >>> al estar extendiendo un diccionario propiamente dicho, aquí si puedes
> hacer la llamada al padre.
> >>>
> >>> Un saludo,
> >>> Vicente Ruiz
> >>>
> >>> El 12 de abril de 2012 09:23, Andrey Antoukh <andsux at gmail.com>
> escribió:
> >>>>
> >>>> Usar oldclass hoy en día debe ser un crimen penal! xD
> >>>>
> >>>> Andrei. ;)
> >>>>
> >>>> El 12 de abril de 2012 09:13, Manuel de la Peña <etil15 at gmail.com>
> escribió:
> >>>>
> >>>>>
> >>>>> On Apr 12, 2012 8:54 AM, "Vicente Ruiz" <vruiz2.0 at gmail.com> wrote:
> >>>>> >
> >>>>> > Hola Miguel Ángel,
> >>>>> >
> >>>>> > es una pregunta muy buena :) La verdad es que me ha costado un
> rato dar con el problema. Prueba cambiar esto:
> >>>>> >
> >>>>> > class EasyDict: --> class EasyDict(object):
> >>>>> >
> >>>>> > Esto se debe a que en las versiones anteriores a Python 3.x
> existen 2 tipos de clases: new-style y old-style. Esto desaparece en Python
> 3, siendo todas new-style. Puedes leer más sobre el tema aquí:
> http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes
> >>>>> >
> >>>>> > Un saludo,
> >>>>> > Vicente Ruiz
> >>>>>
> >>>>> Buenas,
> >>>>>
> >>>>> Acá van mis 2c, la implementación no es totalmente correcta ya que
> tiene los siguientes fallos:
> >>>>>
> >>>>> 1. Estas devolviendo None cuando el valor no está en el dict. Esto
> rompe la interfaz de getattr, lo correcto sería lanzar la excepción
> AtributeError.
> >>>>> 2. No estas llamando al getattr del parental class cosa que es
> probablemente recomendable, yo haría algo como:
> >>>>>
> >>>>> def __getattr__(self, name):
> >>>>>     try:
> >>>>>         return super(Easydict, self).__geattr__(name)
> >>>>>     except AttributeError:
> >>>>>         if name in self.dict:
> >>>>>             return self.dict [name]
> >>>>>         raise AttributeError
> >>>>>
> >>>>> Creo recordar que si era un new class puedes usar super de está
> forma, pero no lo he probado por que estoy en el metro : P.
> >>>>>
> >>>>> Saludos
> >>>>>
> >>>>>
> >>>>> >
> >>>>> > El 12 de abril de 2012 08:25, Kiko <kikocorreoso at gmail.com>
> escribió:
> >>>>> >
> >>>>> >>
> >>>>> >>
> >>>>> >> El 12 de abril de 2012 07:58, Miguel Angel <
> miguelangel.garcia at gmail.com> escribió:
> >>>>> >>
> >>>>> >>> Hola!
> >>>>> >>>
> >>>>> >>> Tengo un problema y no sabía muy bien dónde preguntar... No sé
> si esta lista se utiliza sólo para las reuniones o las preguntas también
> tienen cabida.
> >>>>> >>
> >>>>> >>
> >>>>> >> A priori no hay ningún problema con las preguntas pero la
> finalidad de esta lista es un poco de organización de python-madrid, las
> quedadas y cosas así.
> >>>>> >>
> >>>>> >> Seguro que muchos de esta lista te podrán responder pero para el
> futuro es más adecuado que para las dudas técnicas de python acudas a:
> >>>>> >>
> >>>>> >>  -la lista python-es  (
> http://mail.python.org/mailman/listinfo/python-es).
> >>>>> >>  -a python.majibu (http://python.majibu.org/)
> >>>>> >>
> >>>>> >> Donde hay mucha más gente que en esta lista y cuyo objetivo es
> ese, resolver dudas técnicas
> >>>>> >>
> >>>>> >> Saludos.
> >>>>> >>
> >>>>> >> --
> >>>>> >> Grupo de usuarios de python ubicados en Madrid
> >>>>> >> http://groups.google.es/group/python-madrid
> >>>>> >> http://python-madrid.es
> >>>>> >> http://twitter.com/#!/python_madrid
> >>>>> >> Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> > --
> >>>>> > Disponible en:
> >>>>> >
> >>>>> > http://www.vicenteruiz.es/
> >>>>> > http://www.menudoproblema.es/
> >>>>> > Twitter - LinkedIn
> >>>>> >
> >>>>> > --
> >>>>> > Grupo de usuarios de python ubicados en Madrid
> >>>>> > http://groups.google.es/group/python-madrid
> >>>>> > http://python-madrid.es
> >>>>> > http://twitter.com/#!/python_madrid
> >>>>> > Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
> >>>>>
> >>>>> --
> >>>>> Grupo de usuarios de python ubicados en Madrid
> >>>>> http://groups.google.es/group/python-madrid
> >>>>> http://python-madrid.es
> >>>>> http://twitter.com/#!/python_madrid
> >>>>> Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
> >>>>
> >>>>
> >>>>
> >>>>
> >>>> --
> >>>> Andrei Antoukh - <niwi at niwi.be>
> >>>> http://www.niwi.be/page/about/
> >>>> http://www.kaleidos.net/A5694F/
> >>>>
> >>>> "Linux is for people who hate Windows, BSD is for people who love
> UNIX"
> >>>> "Social Engineer -> Because there is no patch for human stupidity"
> >>>>
> >>>> --
> >>>> Grupo de usuarios de python ubicados en Madrid
> >>>> http://groups.google.es/group/python-madrid
> >>>> http://python-madrid.es
> >>>> http://twitter.com/#!/python_madrid
> >>>> Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
> >>>
> >>>
> >>>
> >>>
> >>> --
> >>> Disponible en:
> >>>
> >>> http://www.vicenteruiz.es/
> >>> http://www.menudoproblema.es/
> >>> Twitter - LinkedIn
> >>>
> >>> --
> >>> Grupo de usuarios de python ubicados en Madrid
> >>> http://groups.google.es/group/python-madrid
> >>> http://python-madrid.es
> >>> http://twitter.com/#!/python_madrid
> >>> Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
> >>
> >> --
> >> Grupo de usuarios de python ubicados en Madrid
> >> http://groups.google.es/group/python-madrid
> >> http://python-madrid.es
> >> http://twitter.com/#!/python_madrid
> >> Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
> >
> >
> >
> >
> > --
> > Disponible en:
> >
> > http://www.vicenteruiz.es/
> > http://www.menudoproblema.es/
> > Twitter - LinkedIn
> >
> > --
> > Grupo de usuarios de python ubicados en Madrid
> > http://groups.google.es/group/python-madrid
> > http://python-madrid.es
> > http://twitter.com/#!/python_madrid
> > Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
>
> --
> Grupo de usuarios de python ubicados en Madrid
> http://groups.google.es/group/python-madrid
> http://python-madrid.es
> http://twitter.com/#!/python_madrid
> Para darte de baja del grupo envía un mensaje a
> python-madrid+unsubscribe at googlegroups.com
>



-- 
Andrei Antoukh - <niwi at niwi.be>
http://www.niwi.be/page/about/
http://www.kaleidos.net/A5694F/

"Linux is for people who hate Windows, BSD is for people who love UNIX"
"Social Engineer -> Because there is no patch for human stupidity"

-- 
Grupo de usuarios de python ubicados en Madrid
http://groups.google.es/group/python-madrid
http://python-madrid.es
http://twitter.com/#!/python_madrid
Para darte de baja del grupo envía un mensaje a python-madrid+unsubscribe at googlegroups.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.es.python.org/pipermail/madrid/attachments/20120412/e3aa219a/attachment.html>


More information about the Madrid mailing list