I need newbie help

J

Joseph Davidson

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]

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

It seems that this is setting up fileLocation as a dictionary, but what
is the "[location]" doing?

Joe Davidson

--
 
S

Sean Berry

Joseph Davidson said:
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

--
 
J

john fabiani

Sean said:
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
 
S

Sean Berry

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 said:
Sean said:
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
 
P

Paul McNett

Sean said:
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."

What's the matter with using if...elif...else:

if case1: # do something
elif case2: # do something else
elif case3: # do something else
else: # do the default something else
 
A

Andrew Bennetts

]
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".

You seem to be a little bit confused, which is likely to make a newbie
really confused.

What is x in your example? Your dictionary's definition depends on multiple
expressions involving it -- you'd only get the result of selector == 'return
1' when a is 1 if 0 <= x < 1.

For instance:
.... x<0 : 'return None',
.... 0<=x<1 : 'return 1',
.... 1<=x<2 : 'return 2',
.... 2<=x<3 : 'return 3',
.... 3<=x : 'return None'
.... }
{False: 'return None', True: 'return 1'}

But:
.... x<0 : 'return None',
.... 0<=x<1 : 'return 1',
.... 1<=x<2 : 'return 2',
.... 2<=x<3 : 'return 3',
.... 3<=x : 'return None'
.... }
{False: 'return None', True: 'return 3'}

So looking up 1 in that dictionary would return whatever string was
associated with the key True (because 1 == True), which is entirely
dependent on the value of x when the dictionary was defined.

Also, your use of strings containing python statements is misleading --
retrieving a value from a dictionary doesn't implicitly execute it as a
string of Python source code too. Less confusing values would have been
'less than 0', 'between 0 and 1', and so on, because at least that is less
likely to confuse a newbie.

Finally, as someone else pointed out, often the best way to do a switch
statement in Python is not to be clever at all, but instead to just simply
do:

if x < 0:
return None
elif 0 <= x < 1:
return 1
elif 1 <= x < 2:
return 2
elif 2 <= x < 3:
return 3
else:
return None

Although that's a very contrived example (what's the point of doing that?)
-- more convincing would be something like:

if colour == 'red':
r, g, b = 255, 0, 0
elif colour == 'purple':
r, g, b = 255, 0, 255

# and so on ...

Which I think turns out to be a much better candidate for the dictionary
trick anyway:

rgbValues = {
'red': (255, 0, 0),
'purple': (255, 0, 255),
# ...
}

r, g, b = rgbValues[colour]

-Andrew.
 
S

Sean Berry

Sorry for any confusion.

I didn't write this page...
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/269708
I was just trying to point out what they were trying to show.



Andrew Bennetts said:
]
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".

You seem to be a little bit confused, which is likely to make a newbie
really confused.

What is x in your example? Your dictionary's definition depends on multiple
expressions involving it -- you'd only get the result of selector == 'return
1' when a is 1 if 0 <= x < 1.

For instance:
... x<0 : 'return None',
... 0<=x<1 : 'return 1',
... 1<=x<2 : 'return 2',
... 2<=x<3 : 'return 3',
... 3<=x : 'return None'
... }
{False: 'return None', True: 'return 1'}

But:
... x<0 : 'return None',
... 0<=x<1 : 'return 1',
... 1<=x<2 : 'return 2',
... 2<=x<3 : 'return 3',
... 3<=x : 'return None'
... }
{False: 'return None', True: 'return 3'}

So looking up 1 in that dictionary would return whatever string was
associated with the key True (because 1 == True), which is entirely
dependent on the value of x when the dictionary was defined.

Also, your use of strings containing python statements is misleading --
retrieving a value from a dictionary doesn't implicitly execute it as a
string of Python source code too. Less confusing values would have been
'less than 0', 'between 0 and 1', and so on, because at least that is less
likely to confuse a newbie.

Finally, as someone else pointed out, often the best way to do a switch
statement in Python is not to be clever at all, but instead to just simply
do:

if x < 0:
return None
elif 0 <= x < 1:
return 1
elif 1 <= x < 2:
return 2
elif 2 <= x < 3:
return 3
else:
return None

Although that's a very contrived example (what's the point of doing that?)
-- more convincing would be something like:

if colour == 'red':
r, g, b = 255, 0, 0
elif colour == 'purple':
r, g, b = 255, 0, 255

# and so on ...

Which I think turns out to be a much better candidate for the dictionary
trick anyway:

rgbValues = {
'red': (255, 0, 0),
'purple': (255, 0, 255),
# ...
}

r, g, b = rgbValues[colour]

-Andrew.
 
N

Nick Vargish

Paul McNett said:
What's the matter with using if...elif...else:

In the example, I think the advantage is that the dictionary lookup is
data driven. If you want to add a new location, you just extend the
dictionary, which is cleaner than adding another clause to an
if...elif block.

Also, if you want to change the name of a location, you just change
the relevant data -- not the code.

Nick
 
J

john fabiani

Sean said:
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".








Sean Berry wrote:

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
Thanks it's a replacement for a switch statement. This is cool. Is
"[a]" a list? If it's a list does it allow complex statement (not that
I can think of one at the moment)?
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top