Python Forum
Is there a something like "case" in Python 3.6?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a something like "case" in Python 3.6?
#11
(Feb-25-2017, 12:18 PM)wavic Wrote: As @ichabod801 said the Python functions are first-class objects. I don't know what first-class means

A first-class object is an object that has no restrictions on it's use. It can be assigned as a value to a variable or attribute, it can be passed to a function as a parameter, it can be returned as a value from a function, and so on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Thank you for this explanation!  Smile  I can understand now some other things. And why they are possiible
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
(Feb-25-2017, 11:35 AM)tdow6478 Wrote: That is ingenious. I would have thought an eval() statement would have been required to make that work. The more I deal with Python the more I like it.

Not specific to Python. You can do this in any language where you have a decent Map/Dictionary support (about all, nowadays).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#14
(Feb-25-2017, 11:22 AM)ichabod801 Wrote:
(Feb-25-2017, 10:33 AM)Raptor88 Wrote: Reply to zivoni:  If there is some short code for the "dictionary mapping" approach that you can post, or a link to a simple explanation, would appreciate that.

Mekire posted some code (four posts up). Basically, the keys of the dictionary are the values you would switch on. The values of the dictionary are the results of the switch. Since Python functions are first class objects, and can be values in dictionaries, you can get rather complicated behavior using a dictionary this way.

Oh, I thought zivoni's comment about the "dictionary mapping" approach was an alternative to what Mekire had posted.

Newbie error on my part.

(Feb-25-2017, 11:33 AM)snippsat Wrote:
(Feb-25-2017, 10:33 AM)Raptor88 Wrote: If there is some short code for the "dictionary mapping" approach that you can post, or a link to a simple explanation, would appreciate that.
Can make one more:
color = 'red' # From eg user input
if color == 'red':
   set_color = 'rgb(255,0,0)'
elif color == 'blue':
   set_color = 'rgb(0,0,255)'
elif color == 'green':
   set_color = 'rgb(0,128,0)'
else:
   print('No rgb for that color')

print(set_color) #--> rgb(255,0,0)
With dictionary and get() method.
color = 'red' # From eg user input
choices = {'red': 'rgb(255,0,0)', 'blue': 'rgb(0,0,255)', 'green': 'rgb(0,128,0)'}
set_color = choices.get(color, 'No rgb for that color')
print(set_color) #--> rgb(255,0,0)

snippsat,

Thank you for taking the time to key in the comparison code.  Very helpful for a newbie to try to understand what's happening.  Studying every line now.

Thanks.

(Feb-25-2017, 12:18 PM)wavic Wrote: As @ichabod801 said the Python functions are first-class objects. I don't know what first-class means but I know that I can make a reference to a function. For example print():

In [1]: say = print

In [2]: def yes_ans():
   ...:     say("OK, wiping C: drive in progress. It will take some time.")
   ...:     

In [3]: def no_ans():
   ...:     say("OK, C: drive zero fill is starting now. Be patient!")
   ...:     

In [4]: action = {'yes': yes_ans, 'no': no_ans}

In [5]: while True:
   ...:     answer = input("Would you like to free some space on C:?[yes/no]: ")
   ...: 
   ...:     if answer.lower() in action:
   ...:         action[answer.lower()]()
   ...:         break
   ...:     
Would you like to free some space on C:?[yes/no]: no
OK, C: drive zero fill is starting now. Be patient!
Here the references to yes_ans and no_ans are stored in a dictionary and you check for the key then get the value of the key which is the function name. To call the function you have to put brackets after the dict[function]: dict[function]()

Studying your example.  What's the reason for all of the leading "...:" dots and colon?  Have not seen that in any Python tutorial.
Thanks.
Reply
#15
(Feb-26-2017, 06:40 PM)Raptor88 Wrote: Studying your example.  What's the reason for all of the leading "...:" dots and colon?  Have not seen that in any Python tutorial.
Thanks.

Probably IPython or Jupyter console. Its interactive python shell with nice features like syntax highlighting, tab completion, multiline editing.
Reply
#16
It's IPython. This is like the Python interpreter with extras. The dots are just an indication that this a is code block or multiple lines as an input from paste command for example.. The prompt is these in/out[number]: things.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#17
(Feb-26-2017, 08:47 PM)zivoni Wrote:
(Feb-26-2017, 06:40 PM)Raptor88 Wrote: Studying your example.  What's the reason for all of the leading "...:" dots and colon?  Have not seen that in any Python tutorial.
Thanks.

Probably IPython or Jupyter console. Its interactive python shell with nice features like syntax highlighting, tab completion, multiline editing.

Thanks for explaining.
Raptor88

(Feb-26-2017, 08:53 PM)wavic Wrote: It's IPython. This is like the Python interpreter with extras. The dots are just an indication that this a is code block or multiple lines as an input from paste command for example.. The prompt is these in/out[number]: things.

Thanks for explaining.
Raptor88
Reply
#18
You can try Jupyter Notebook in browser.
There are a lot of cool stuff that can be made with it.

There are several REPL for Python,the default one show up when type python in shell.
The Notebook is like the most advance REPL.
I like ptpython for my fast interactive testing,a review here.
Reply
#19
(Feb-26-2017, 10:35 PM)snippsat Wrote: You can try Jupyter Notebook in browser.
There are a lot of cool stuff that can be made with it.

There are several REPL for Python,the default one show up when type python in shell.
The Notebook is like the most advance REPL.
I like ptpython for my fast interactive testing,a review here.

Recently started using PyCharm and learning that will keep me busy for quite a while.  Will remember your tips for future use though.

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What colon (:) in Python mean in this case? Yapwc 4 2,146 Dec-28-2022, 04:04 PM
Last Post: snippsat
  Switch case or match case? Frankduc 9 4,493 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,663 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 1,862 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to use switch/case in python? newbieguy 9 4,045 Nov-08-2019, 11:35 AM
Last Post: newbieguy
  How to write switch case statement in Python pyhelp 9 9,183 Nov-11-2018, 08:53 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020