I need newbie help
Sean Berry
sean_berry at cox.net
Fri May 14 01:16:01 EDT 2004
You have to read the whole page to understand why.
The title is: Python style switches.
Python does not have a swith statement like many other languages.
A switch statement could look like this:
switch (a)
case 1: do something
case 2: do something else
case 3: do something else
default: do something else
It evaluates a and checks to see if there is a matching case. If there
is, it executes the code for that case. So if a==1, it would "do
something."
Since Python doesn't have a switch statement, you can make your own.
In Python you could do something like this though:
selector={
x<0 : 'return None',
0<=x<1 : 'return 1',
1<=x<2 : 'return 2',
2<=x<3 : 'return 3',
3<=x : 'return None'
}[a]
if a == 1, it would "return 1".
"john fabiani" <jfabiani at yolo.com> wrote in message
news:F0Yoc.67040$Ub.57241 at newssvr25.news.prodigy.com...
> Sean Berry wrote:
>
> > "Joseph Davidson" <jhd at radix.net> wrote in message
> > news:10a83t8qku40d44 at corp.supernews.com...
> >
> >>I am an experienced perl programmer learning python ( no commente please
> >>).
> >>
> >>In looking at a sample script at
> >>http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/269708
> >>
> >>I found the following code snippet.
> >>--------------------------------------------------
> >>location = 'myHome'
> >>fileLocation = {'myHome' :path1,
> >> 'myOffice' :path2,
> >> 'somewhere':path3}[location]
> >>
> >>-----------------------------------------------------
> >>
> >
> >
> > fileLocation takes the 'value' of the 'key' "location"
> > equilvalent to:
> >
> > location = 'myHome'
> > files = {'myHome' :path1, 'myOffice' :path2, 'somewhere':path3}
> > fileLocation = files[location]
> >
> >
> >>It seems that this is setting up fileLocation as a dictionary, but what
> >>is the "[location]" doing?
> >>
> >>Joe Davidson
> >>
> >>--
> >
> >
> >
> I'm also a newbie. Did the programmer that wrote the code not use your
> way (3 statements) because his statement was faster or because it better
> python?
>
> John
More information about the Python-list
mailing list