beginner, idomatic python 2

Dan Bishop danb_83 at
Fri Aug 24 05:49:48 CEST 2007

On Aug 23, 10:21 pm, "bambam" <da... at asdf.asdf> wrote:
> Would someone like to suggest a replacement for this? This is a
> function that returns different kinds of similar objects, depending
> on what is asked for. PSP and PWR are classes.  I don't really
> want to re-write the calling code very much: I'm just wondering
> if the function can be replaced with some kind of OOP pattern.
> def Device(DeviceType):
>     if DeviceType=='PSP':
>         return PSP()
>     elif DeviceType=="Power Supply"
>         return PWR()
>     etc...
> Thanks!

Typically, you'd use a dictionary:

    'PSP': PSP.
    'Power Supply': PWR,
    # etc.

and your function would simply return DEVICE_DICT[device_type]()

More information about the Python-list mailing list