beginner, idomatic python 2

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Aug 24 06:18:11 EDT 2007


bambam a écrit :
>> trying to be "idiomatic"
> 
> ...I hope that if a python programmer looks at my code it
> won't be an excuse to discard it. 

Hopefully not - and nothing forces you into adopting the common 
convention !-) But it's a fact that Python relies heavily on naming 
conventions, and that it greatly helps readability.

> Less of an issue with Python
> than with C/C++, but since I'm just starting...
> 
> def device(DeviceType):
>     if DeviceType=='PSP':
>         return Psp()
>     elif DeviceType=="Power Supply"
>         return Pwr()
> 
> What about the parameter "DeviceType"?

DEVICE_TYPES = {
   'PSP' : Psp,
   'Power Supply' : Pwr,
}

def get_device(device_type):
     cls = DEVICE_TYPES.get(device_type, None)
     if cls is not None:
        return cls()
     # implicitly returns None, which may or not be a GoodThing(tm)



HTH



More information about the Python-list mailing list