Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66792 > unrolled thread

Can global variable be passed into Python function?

Started bySam <lightaiyee@gmail.com>
First post2014-02-20 22:37 -0800
Last post2014-03-01 15:22 +1100
Articles 20 on this page of 217 — 27 participants

Back to article view | Back to comp.lang.python


Contents

  Can global variable be passed into Python function? Sam <lightaiyee@gmail.com> - 2014-02-20 22:37 -0800
    Re: Can global variable be passed into Python function? dieter <dieter@handshake.de> - 2014-02-21 08:23 +0100
      Re: Can global variable be passed into Python function? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-02-21 10:55 +0200
        Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-21 12:10 +0200
    Re: Can global variable be passed into Python function? Peter Otten <__peter__@web.de> - 2014-02-21 08:34 +0100
    Re: Can global variable be passed into Python function? Gary Herron <gary.herron@islandtraining.com> - 2014-02-21 00:41 -0800
    Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-21 08:55 +0000
    Re: Can global variable be passed into Python function? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-21 07:13 -0500
      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-21 14:52 +0200
      Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 03:28 +0000
        Re: Can global variable be passed into Python function? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-21 22:45 -0500
          Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 06:29 +0000
    Re: Can global variable be passed into Python function? Travis Griggs <travisgriggs@gmail.com> - 2014-02-21 09:59 -0800
    Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-22 05:16 +1100
      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-21 21:20 +0200
        Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-22 17:36 +1100
          Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 07:18 +0000
            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-22 18:29 +1100
              Re: Can global variable be passed into Python function? wxjmfauth@gmail.com - 2014-02-22 00:02 -0800
                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-22 19:10 +1100
                  Re: Can global variable be passed into Python function? wxjmfauth@gmail.com - 2014-02-22 00:26 -0800
                  Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 08:28 +0000
              Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 08:35 +0000
                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-22 19:45 +1100
                Re: Can global variable be passed into Python function? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-24 21:07 +1300
                  Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-24 15:57 +0000
                    Re: Can global variable be passed into Python function? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-24 21:12 -0500
                    Re: Can global variable be passed into Python function? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-26 23:59 +1300
                      Re: Can global variable be passed into Python function? MRAB <python@mrabarnett.plus.com> - 2014-02-26 18:59 +0000
        Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 06:57 +0000
          Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-22 09:28 +0200
            Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-22 08:45 +0000
              Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-22 19:54 +1100
    Python and variables (was: Can global variable be passed into Python function?) Ben Finney <ben+python@benfinney.id.au> - 2014-02-22 11:13 +1100
    Re: Can global variable be passed into Python function? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-21 21:47 -0500
    Re: Can global variable be passed into Python function? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-21 22:14 -0500
    Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-22 14:15 +0000
      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-22 16:44 +0200
      Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-23 01:39 +0000
        Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-23 12:50 +1100
          Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-23 06:20 +0000
            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-23 18:23 +1100
        Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-23 11:52 +0200
          Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-23 10:30 +0000
          Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-23 21:32 +1100
            Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-23 13:01 +0200
              Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-23 22:12 +1100
                Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-23 17:24 +0200
                  Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-24 02:41 +1100
                    Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-23 23:04 +0200
                      Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-23 21:18 +0000
              Re: Can global variable be passed into Python function? Terry Reedy <tjreedy@udel.edu> - 2014-02-23 12:06 -0500
                Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-23 23:10 +0200
                  Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-24 00:37 +0000
                    Re: Can global variable be passed into Python function? wxjmfauth@gmail.com - 2014-02-24 01:35 -0800
              Re: Can global variable be passed into Python function? j.e.haque@gmail.com - 2014-02-24 10:05 -0800
                Re: [OT] Can global variable be passed into Python function? Michael Torrie <torriem@gmail.com> - 2014-02-24 11:19 -0700
                  Re: [OT] Can global variable be passed into Python function? Grant Edwards <invalid@invalid.invalid> - 2014-02-24 19:42 +0000
                    Re: [OT] Can global variable be passed into Python function? Dave Angel <davea@davea.name> - 2014-03-01 23:02 -0500
                      Re: [OT] Can global variable be passed into Python function? Grant Edwards <invalid@invalid.invalid> - 2014-03-02 16:45 +0000
                        Re: [OT] Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 16:55 +0000
                        Re: [OT] Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-03 06:24 +1100
                          Re: [OT] Can global variable be passed into Python function? Grant Edwards <invalid@invalid.invalid> - 2014-03-03 14:18 +0000
                            Re: [OT] Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-04 01:25 +1100
                    Re: [OT] Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 15:17 +1100
                    Re: [OT] Can global variable be passed into Python function? Dave Angel <davea@davea.name> - 2014-03-02 08:22 -0500
                    Re: [OT] Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-03 00:57 +1100
                    Re: [OT] Can global variable be passed into Python function? Dave Angel <davea@davea.name> - 2014-03-02 14:17 -0500
                    Re: [OT] Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-03 06:32 +1100
                    Re: [OT] Can global variable be passed into Python function? Dave Angel <davea@davea.name> - 2014-03-02 17:58 -0500
                Re: Can global variable be passed into Python function? random832@fastmail.us - 2014-02-24 13:20 -0500
                Re: [OT] Can global variable be passed into Python function? random832@fastmail.us - 2014-02-24 13:21 -0500
                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-25 05:22 +1100
                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-25 05:25 +1100
                Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-24 20:00 +0000
    Re: Can global variable be passed into Python function? Dave Angel <davea@davea.name> - 2014-02-22 10:02 -0500
    Re: Can global variable be passed into Python function? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-22 13:03 -0500
      Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-23 00:39 +0000
      Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-02-22 19:57 -0500
    Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 05:24 -0800
      Re: Can global variable be passed into Python function? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-27 12:54 -0500
        Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 15:29 -0800
          Re: Can global variable be passed into Python function? Steven D'Aprano <steve@pearwood.info> - 2014-02-28 02:07 +0000
            Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 18:29 -0800
              Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 15:43 +1100
                Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 21:39 -0800
                  Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 16:53 +1100
                Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 09:43 +0200
                  Re: Can global variable be passed into Python function? Steven D'Aprano <steve@pearwood.info> - 2014-02-28 08:23 +0000
                    Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-02-28 19:46 +1100
                      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 12:02 +0200
                        Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 21:55 +1100
                          Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 13:30 +0200
                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 22:51 +1100
                              Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 14:25 +0200
                                Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 00:22 +0000
                        Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 22:08 +1100
                          Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 13:38 +0200
                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 23:22 +1100
                            Re: Can global variable be passed into Python function? Neil Cerutti <neilc@norwich.edu> - 2014-02-28 13:47 +0000
                              Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 16:26 +0200
                                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 01:37 +1100
                                  Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 17:29 +0200
                                    Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 02:46 +1100
                                    Re: Can global variable be passed into Python function? Grant Edwards <invalid@invalid.invalid> - 2014-02-28 16:09 +0000
                                      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 10:00 +0200
                                Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-02-28 09:43 -0500
                                  Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 10:00 +0200
                                    Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 17:26 +0200
                                      Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-03-02 10:34 -0500
                                        Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 17:52 +0200
                                          Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-03 03:23 +1100
                                          Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 16:53 +0000
                            Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-28 15:06 +0000
                        Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-28 15:50 +0000
                          Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:04 -0800
                            Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 20:53 +0200
                              Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 05:59 +1100
                                Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 21:20 +0200
                                  Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 12:22 -0800
                                    Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-02-28 23:03 +0200
                                      Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-28 21:23 +0000
                                        Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 01:06 +0200
                                          Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 11:02 +1100
                                            Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-02-28 19:48 -0500
                                          Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 13:00 +1100
                                            Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-02-28 22:15 -0500
                                        Re: Can global variable be passed into Python function? Grant Edwards <invalid@invalid.invalid> - 2014-02-28 22:00 +0000
                                          Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-02 09:36 +1100
                                            Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 14:50 -0800
                                      Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 15:36 -0800
                                        Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 02:32 +0200
                                        Re: Can global variable be passed into Python function? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-28 19:40 -0500
                                          Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 17:08 -0800
                                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 13:01 +1100
                                              Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 21:30 -0800
                                                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 17:24 +1100
                                                  Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 13:40 -0800
                                                    Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 22:01 +0000
                                                      Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 14:07 -0800
                                                        Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 23:21 +0000
                                                          Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 16:23 -0800
                                                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 11:28 +1100
                                                            Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 00:36 +0000
                                                            Re: Can global variable be passed into Python function? Ned Deily <nad@acm.org> - 2014-03-01 16:55 -0800
                                                            Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 01:15 +0000
                                                    Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 09:11 +1100
                                            Re: Can global variable be passed into Python function? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-28 21:15 -0500
                                              Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 20:36 -0800
                                        Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 01:07 +0000
                                        Re: Can global variable be passed into Python function? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-01 16:10 +1300
                                        Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-02-28 19:02 -0500
                                  Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-28 23:33 +0000
                                  Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 12:50 +1100
                              Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-28 23:04 +0000
                                Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 02:03 +0200
                                  Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 00:44 +0000
                                    Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 03:06 +0200
                                      Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 01:59 +0000
                                        Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 13:03 +1100
                                          Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 03:29 +0000
                                        Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 12:39 +0200
                                  Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 01:10 +0000
                                    Re: Can global variable be passed into Python function? "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 17:29 -0800
                        Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 10:17 +1100
                          Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 02:11 +0200
                            Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 11:50 +1100
                              Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 03:10 +0200
                                Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 01:19 +0000
                                Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 12:41 +1100
                                  Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 12:31 +0200
                                    Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 21:48 +1100
                                      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 13:28 +0200
                                        Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-01 22:59 +1100
                                          Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 17:07 +0000
                                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 04:27 +1100
                                            Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 20:25 +0200
                                              Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-02 09:30 +1100
                                              Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 23:13 +0000
                                        Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-02 00:03 +1100
                                          Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 19:23 +0200
                                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 04:30 +1100
                                            Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-02 09:34 +1100
                                              Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 10:03 +0200
                                                Re: Can global variable be passed into Python function? Ben Finney <ben+python@benfinney.id.au> - 2014-03-02 21:59 +1100
                                                Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 13:00 +0000
                                        Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 15:15 +0000
                                        Re: Can global variable be passed into Python function? Michael Torrie <torriem@gmail.com> - 2014-03-01 10:05 -0700
                                          Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-01 19:29 +0200
                                            Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 04:36 +1100
                                            Re: Can global variable be passed into Python function? Michael Torrie <torriem@gmail.com> - 2014-03-01 11:06 -0700
                                            Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-03-01 13:30 -0500
                                            Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-02 08:35 +0000
                                              Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 11:35 +0200
                                                Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 11:40 +0200
                                                Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-02 21:07 +1100
                                                  Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 12:37 +0200
                                                Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-02 10:44 +0000
                                                  Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 13:33 +0200
                                                    Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-03 01:50 +0000
                                                Re: Can global variable be passed into Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-02 13:04 +0000
                                                Re: Can global variable be passed into Python function? Michael Torrie <torriem@gmail.com> - 2014-03-02 11:48 -0700
                                                  Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-02 23:03 +0200
                                                    Re: Can global variable be passed into Python function? Roy Smith <roy@panix.com> - 2014-03-02 16:16 -0500
                                                    Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-03-03 08:28 +1100
                                                    Re: Can global variable be passed into Python function? Michael Torrie <torriem@gmail.com> - 2014-03-02 14:44 -0700
                                                      Re: Can global variable be passed into Python function? Marko Rauhamaa <marko@pacujo.net> - 2014-03-03 00:46 +0200
                                    Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 17:18 +0000
                            Re: Can global variable be passed into Python function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 00:58 +0000
                    Re: Can global variable be passed into Python function? Michael Torrie <torriem@gmail.com> - 2014-02-28 07:49 -0700
                  Re: Can global variable be passed into Python function? Chris Angelico <rosuav@gmail.com> - 2014-02-28 20:00 +1100
                  Re: Can global variable be passed into Python function? Grant Edwards <invalid@invalid.invalid> - 2014-02-28 14:20 +0000
          References, and avoiding use of “variable” (was: Can global variable be passed into Python function?) Ben Finney <ben+python@benfinney.id.au> - 2014-02-28 13:46 +1100
          Re: References, and avoiding use of ???variable??? (was: Can global variable be passed into Python function?) Neil Cerutti <neilc@norwich.edu> - 2014-02-28 14:30 +0000
          Re: References, and avoiding use of ???variable??? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 10:33 +1100
            Re: References, and avoiding use of ???variable??? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-01 16:08 +1300
              Re: References, and avoiding use of ???variable??? Ben Finney <ben+python@benfinney.id.au> - 2014-03-01 15:22 +1100

Page 5 of 11 — ← Prev page 1 … 3 4 [5] 6 7 … 11  Next page →


#67161

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-02-27 12:54 -0500
Message-ID<mailman.7435.1393523704.18130.python-list@python.org>
In reply to#67142
On 2/27/14 8:24 AM, Mark H. Harris wrote:
>
> As others have noted, python does not have a 'variable' concept (references to objects instead) and so your question is a little ambiguous.
>

Mark, thanks for helping to answer the OP's question.  We've covered (in 
depth) in the rest of this thread, that Python *does* have the concept 
of a variable, it just behaves differently than some other popular 
programming languages (but exactly the same as some other popular 
programming languages!)

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#67177

From"Mark H. Harris" <harrismh777@gmail.com>
Date2014-02-27 15:29 -0800
Message-ID<5f4f5a5f-327a-4616-8235-17ee9e74c488@googlegroups.com>
In reply to#67161
On Thursday, February 27, 2014 11:54:44 AM UTC-6, Ned Batchelder wrote:

> Mark, thanks for helping to answer the OP's question.  We've covered (in 
> depth) in the rest of this thread, that Python *does* have the concept 
> of a variable, it just behaves differently than some other popular  
> programming languages (but exactly the same as some other popular 
> programming languages!)
> 

I do not disagree.  I've had the same discussion over the years since using python, and often its a rope soaking contest...  some folks get so enamored with the 'way' it works under the covers that they forget how people think about it as they are trying (as normal people) to use the language (instead of BASIC). 

So, yeah,  thinking about variables is just not going away.

I finally decided (in my own head) that I would completely give up on the 'variable' concept (intellectually)  and help folks try to understand references and reference counting. Knowing that A points to an int, and that B=A, now B points to the VERY SAME int...  they are references pointing to the same piece of memory. And of course we want new folks to understand the issue of:
A==B
True
A is B
False
    .....   and WHY that is or isn't....    

:)     

it just helps to get the 'variable' idea out of here... it really is something completely different in python.

Thanks for the note,   
kind regards,
marcus

[toc] | [prev] | [next] | [standalone]


#67184

FromSteven D'Aprano <steve@pearwood.info>
Date2014-02-28 02:07 +0000
Message-ID<530fef58$0$11113$c3e8da3@news.astraweb.com>
In reply to#67177
On Thu, 27 Feb 2014 15:29:01 -0800, Mark H. Harris wrote:

> Knowing that A points to an int, and
> that B=A, now B points to the VERY SAME int...  they are references
> pointing to the same piece of memory. And of course we want new folks to
> understand the issue of: A==B
> True
> A is B
> False
>     .....   and WHY that is or isn't....


If they point to the same piece of memory -- which, by the way, can be 
moved around if the garbage collector supports it -- then A is B cannot 
possibly return False.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#67186

From"Mark H. Harris" <harrismh777@gmail.com>
Date2014-02-27 18:29 -0800
Message-ID<e24e0817-67d1-4dbd-b3ba-9250e735d261@googlegroups.com>
In reply to#67184
On Thursday, February 27, 2014 8:07:20 PM UTC-6, Steven D'Aprano wrote:

> If they point to the same piece of memory -- which, by the way, can be 
> moved around if the garbage collector supports it -- then A is B cannot  
> possibly return False.
> 

hi Steve, long time,   yes, my whole point exactly.  And we all know what python is doing under the covers for small ints   like  0 - 256  ...   in which case consider the following:

a=128
b=a
b=128
a is b
True

But......   consider this

a=1024
b=a
b=1024
a is b
False

For small ints below a certain value (257)  A is B  will always be True....   but now for ints above that value, as I've shown, 
A=257
B=A
B=257
A is B
False

But for the variable discussion (like in BASIC, or even C)  A=128, B=A,  A and B are two pieces of memory that both happen to be variables containing different pieces of memory.  For python, the references to small ints (as above) will almost always point to the same int object for each reference; hence the need for reference counts.   

Folks that think of assignment in python with a BASIC, or even C, mentality will have trouble understanding the difference between  "=="  and "is"  and why...   and they won't get reference counting.

Cheers

[toc] | [prev] | [next] | [standalone]


#67194

FromChris Angelico <rosuav@gmail.com>
Date2014-02-28 15:43 +1100
Message-ID<mailman.7453.1393562612.18130.python-list@python.org>
In reply to#67186
On Fri, Feb 28, 2014 at 1:29 PM, Mark H. Harris <harrismh777@gmail.com> wrote:
> a=1024
> b=a
> b=1024
> a is b
> False

No no no no! They're not pointing to the same integer any more. Now,
if you change the "b=1024" from being a mostly-useless assignment (to
another int with the same value) into being a comparison, then it'll
be safe. But you're assigning "b=a" and then immediately reassigning
"b=1024".

Simple rule of thumb: Never use 'is' with strings or ints. They're
immutable, their identities should be their values. Playing with 'is'
will only confuse you, unless you're specifically going for
introspection and such.

ChrisA

[toc] | [prev] | [next] | [standalone]


#67200

From"Mark H. Harris" <harrismh777@gmail.com>
Date2014-02-27 21:39 -0800
Message-ID<4a7700ed-835c-4e19-8c6d-ab451b696390@googlegroups.com>
In reply to#67194
On Thursday, February 27, 2014 10:43:23 PM UTC-6, Chris Angelico wrote:

> Simple rule of thumb: Never use 'is' with strings or ints. They're
> immutable, their identities should be their values. Playing with 'is'
> will only confuse you, unless you're specifically going for 
> introspection and such.

Right.  The only time I use "is"   is when I'm trying to explain to someone new to python assignment what is happening inside... what Mark Summerfield calls "python's beautiful heart," in his his recent book, "Programming in Python 3" ... a great resource, by the way.

[toc] | [prev] | [next] | [standalone]


#67202

FromChris Angelico <rosuav@gmail.com>
Date2014-02-28 16:53 +1100
Message-ID<mailman.7459.1393566819.18130.python-list@python.org>
In reply to#67200
On Fri, Feb 28, 2014 at 4:39 PM, Mark H. Harris <harrismh777@gmail.com> wrote:
> On Thursday, February 27, 2014 10:43:23 PM UTC-6, Chris Angelico wrote:
>
>> Simple rule of thumb: Never use 'is' with strings or ints. They're
>> immutable, their identities should be their values. Playing with 'is'
>> will only confuse you, unless you're specifically going for
>> introspection and such.
>
> Right.  The only time I use "is"   is when I'm trying to explain to someone new to python assignment what is happening inside... what Mark Summerfield calls "python's beautiful heart," in his his recent book, "Programming in Python 3" ... a great resource, by the way.
>

'is' can and should be used with mutables, to distinguish between
identical values with different identities. It's also appropriate to
use 'is' with special singletons like None. Just be careful of ints
and strings.

ChrisA

[toc] | [prev] | [next] | [standalone]


#67204

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-02-28 09:43 +0200
Message-ID<871tynznpd.fsf@elektro.pacujo.net>
In reply to#67194
Chris Angelico <rosuav@gmail.com>:

> Simple rule of thumb: Never use 'is' with strings or ints. They're
> immutable, their identities should be their values. Playing with 'is'
> will only confuse you, unless you're specifically going for
> introspection and such.

Here's a use case for "is" with strings (or ints):

   class Connection:
       IDLE = "IDLE"
       CONNECTING = "CONNECTING"
       CONNECTED = "CONNECTED"
       DISCONNECTING = "DISCONNECTING"
       DISCONNECTED = "DISCONNECTED"

       def __init__(self):
           self.state = IDLE

       def connect(self, address):
           ...
           self.state = CONNECTING
           ...

       def disconnect(self):
           ...
           if self.state is CONNECTED:
               ...

The state objects could have been defined like this:

       IDLE = object()
       CONNECTING = object()
       CONNECTED = object()
       DISCONNECTING = object()
       DISCONNECTED = object()

However, strings have the advantage in troubleshooting:

           sys.stderr.write("state = {}\n".format(self.state))


Marko

[toc] | [prev] | [next] | [standalone]


#67206

FromSteven D'Aprano <steve@pearwood.info>
Date2014-02-28 08:23 +0000
Message-ID<53104798$0$11113$c3e8da3@news.astraweb.com>
In reply to#67204
On Fri, 28 Feb 2014 09:43:58 +0200, Marko Rauhamaa wrote:

> Chris Angelico <rosuav@gmail.com>:
> 
>> Simple rule of thumb: Never use 'is' with strings or ints. They're
>> immutable, their identities should be their values. Playing with 'is'
>> will only confuse you, unless you're specifically going for
>> introspection and such.
> 
> Here's a use case for "is" with strings (or ints):

I don't think this is a use-case for "is". See below.

>    class Connection:
>        IDLE = "IDLE"
[...]
>        CONNECTED = "CONNECTED"
[...]
>        def disconnect(self):
>            ...
>            if self.state is CONNECTED:
>                ...

Why do you care that the state is *that specific* string, rather than any 
old string with the value "CONNECTED"?

Unless you can explain a good reason why, say, *this* instance 
"CONNECTED" should fail the test, while *that* instance with the same 
value passes, it's not a good use-case for "is".


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#67207

FromBen Finney <ben+python@benfinney.id.au>
Date2014-02-28 19:46 +1100
Message-ID<mailman.7460.1393577227.18130.python-list@python.org>
In reply to#67206
Steven D'Aprano <steve@pearwood.info> writes:

> On Fri, 28 Feb 2014 09:43:58 +0200, Marko Rauhamaa wrote:
> >    class Connection:
> >        IDLE = "IDLE"
> [...]
> >        CONNECTED = "CONNECTED"
> [...]
> >        def disconnect(self):
> >            ...
> >            if self.state is CONNECTED:
> >                ...
>
> Why do you care that the state is *that specific* string, rather than
> any old string with the value "CONNECTED"?

I can think of a reason:

* When you publish the API for the ‘Connection’ class,

* and another party writes code that sets ‘state’ to a string with the
  value ‘"CONNECTED"’,

* and you implemented the check as ‘self.state == "CONNECTED"’,

* and their code works with your class and it goes into production,

* you're then not able to change the expected value without breaking
  that party's code.

On the other hand, if the only value which will work is the one which
the caller gets via ‘Connection.CONNECTED’, then you can change the
implementation later without breaking existing code.


There are two reasons why I think this is *still* not a justification
for using ‘is’ with string values:

First reason: This is better done by making it clear the value is an
arbitrary object that won't be compared for equality. Just use
‘object()’ to creeate each value and be done with it. That's a hack, but
it's better than pretending you'll use the string as a string of text
and then breaking that expectation.

Second reason: This use case is a primary motivation for the Enum
pattern. The Enum pattern is implemented in the standard-library ‘enum’
module, now in Python 3.4 <URL:http://python.org/dev/peps/pep-0435/>.

So, I think Marko's use case is not a justification for comparing string
values with ‘is’.

-- 
 \        “Pinky, are you pondering what I'm pondering?” “Wuh, I think |
  `\   so, Brain, but if we didn't have ears, we'd look like weasels.” |
_o__)                                           —_Pinky and The Brain_ |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#67212

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-02-28 12:02 +0200
Message-ID<87ha7jy2qs.fsf@elektro.pacujo.net>
In reply to#67207
Ben Finney <ben+python@benfinney.id.au>:

> There are two reasons why I think this is *still* not a justification
> for using ‘is’ with string values:
>
> First reason: This is better done by making it clear the value is an
> arbitrary object that won't be compared for equality. Just use
> ‘object()’ to creeate each value and be done with it. That's a hack,
> but it's better than pretending you'll use the string as a string of
> text and then breaking that expectation.
>
> Second reason: This use case is a primary motivation for the Enum
> pattern. The Enum pattern is implemented in the standard-library
> ‘enum’ module, now in Python 3.4
> <URL:http://python.org/dev/peps/pep-0435/>.
>
> So, I think Marko's use case is not a justification for comparing
> string values with ‘is’.

Yes, enums are long overdue. However, since any distinct objects will
do, there is nothing preventing you from using string objects.


Marko

PS On the topic of enums, when are we getting support for a switch
statement?

[toc] | [prev] | [next] | [standalone]


#67213

FromChris Angelico <rosuav@gmail.com>
Date2014-02-28 21:55 +1100
Message-ID<mailman.7464.1393584957.18130.python-list@python.org>
In reply to#67212
On Fri, Feb 28, 2014 at 9:02 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Yes, enums are long overdue. However, since any distinct objects will
> do, there is nothing preventing you from using string objects.
>

String literals will often be interned if they look like (especially,
if they *are*) identifiers, so if you want to prevent other strings
from happening to match, you can't trust 'is'.

>>> class Foo:
    INIT = "INIT"
    def __init__(self):
        self.state = self.INIT
    def initializing(self):
        return self.state is self.INIT
>>> a=Foo()
>>> a.initializing()
True
>>> a.state="INIT"
>>> a.initializing()
True


So you should use some string value that doesn't look like an identifier:

>>> class Foo:
    INIT = "<<INIT>>"
    def __init__(self):
        self.state = self.INIT
    def initializing(self):
        return self.state is self.INIT
>>> a=Foo()
>>> a.initializing()
True
>>> a.state="<<INIT>>"
>>> a.initializing()
False

But even then, chances are you can force the matter by interning explicitly.

>>> class Foo:
    INIT = ">>INIT<<"
    def __init__(self):
        self.state = self.INIT
    def initializing(self):
        return self.state is self.INIT
>>> a=Foo()
>>> a.initializing()
True
>>> sys.intern(a.state)
'>>INIT<<'
>>> a.state=sys.intern(">>INIT<<")
>>> a.initializing()
True

Note that in no case did I at all tamper with the class definition,
either to change its idea of the INIT string or to fetch that
particular object. Two equal strings, in Python, might and might not
be identical, and you simply cannot rely on interning either way.

The third example, incidentally, depends on sys.intern reusing a.state
as the "one interned string". This will normally be what happens if
it's the first string of that value to be used. So you might be able
to first force the strings to be in the interning table, and then
force your sentinels to be different objects. But at that point, you
really should be using object(), or a proper enum module.

If you're using strings as state values, you should be using == to
compare them. Nothing else is safe.

ChrisA

[toc] | [prev] | [next] | [standalone]


#67215

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-02-28 13:30 +0200
Message-ID<8738j3xynr.fsf@elektro.pacujo.net>
In reply to#67213
Chris Angelico <rosuav@gmail.com>:

> String literals will often be interned if they look like (especially,
> if they *are*) identifiers, so if you want to prevent other strings
> from happening to match, you can't trust 'is'.
>
> [...]
>
> If you're using strings as state values, you should be using == to
> compare them. Nothing else is safe.

You didn't quite understand the use case. You would never ever do things
like:

> >>> a.state="<<INIT>>"

You'd only refer to the state names symbolically:

   a.state = a.INIT


Marko

[toc] | [prev] | [next] | [standalone]


#67217

FromChris Angelico <rosuav@gmail.com>
Date2014-02-28 22:51 +1100
Message-ID<mailman.7466.1393588292.18130.python-list@python.org>
In reply to#67215
On Fri, Feb 28, 2014 at 10:30 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> String literals will often be interned if they look like (especially,
>> if they *are*) identifiers, so if you want to prevent other strings
>> from happening to match, you can't trust 'is'.
>>
>> [...]
>>
>> If you're using strings as state values, you should be using == to
>> compare them. Nothing else is safe.
>
> You didn't quite understand the use case. You would never ever do things
> like:
>
>> >>> a.state="<<INIT>>"
>
> You'd only refer to the state names symbolically:
>
>    a.state = a.INIT

In theory, yes. If that's all people will ever do, then you can safely
use == to check. Why are you using is? To prevent the case where some
other random string will happen to compare equal. So I stuffed some
other random string in, and it was equal, and I proved that I could
make it identical as well.

ChrisA

[toc] | [prev] | [next] | [standalone]


#67219

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-02-28 14:25 +0200
Message-ID<87mwhbwhil.fsf@elektro.pacujo.net>
In reply to#67217
Chris Angelico <rosuav@gmail.com>:

> On Fri, Feb 28, 2014 at 10:30 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Chris Angelico <rosuav@gmail.com>:
>>    a.state = a.INIT
>
> In theory, yes. If that's all people will ever do, then you can safely
> use == to check. Why are you using is?

The main reason to use "is" is to indicate that the object is a sentinel
object whose identity is what is meaningful, not the content. Using ==
would work but would give some poor soul the idea that the state
variable could hold any imaginable string.

The implementation should be able to change the state objects to any
(distinct) objects at all (say, the new enums, or ints, or some more
elaborate class instances) without any changes in the code.


Marko

[toc] | [prev] | [next] | [standalone]


#67261

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-03-01 00:22 +0000
Message-ID<5311285b$0$29985$c3e8da3$5496439d@news.astraweb.com>
In reply to#67219
On Fri, 28 Feb 2014 14:25:54 +0200, Marko Rauhamaa wrote:

> Chris Angelico <rosuav@gmail.com>:
> 
>> On Fri, Feb 28, 2014 at 10:30 PM, Marko Rauhamaa <marko@pacujo.net>
>> wrote:
>>> Chris Angelico <rosuav@gmail.com>:
>>>    a.state = a.INIT
>>
>> In theory, yes. If that's all people will ever do, then you can safely
>> use == to check. Why are you using is?
> 
> The main reason to use "is" is to indicate that the object is a sentinel
> object whose identity is what is meaningful, not the content. Using ==
> would work but would give some poor soul the idea that the state
> variable could hold any imaginable string.

Why should identity be important for testing the state of a state 
variable?

There is a good reason for using "is" with actual sentinel values, 
because we wish to protect against the (rare) situation where some other 
object happens to compare equal to our sentinel. But why should states be 
treated as sentinels instead of ordinary values where identity is not 
important?

The most important reason to *not* use "is" is that doing so *implicitly* 
exposes an implementation detail, namely that each state is a singleton.

Identity shouldn't be that important. Why does it matter that there is 
only a single instance of the state INIT (say)? Why shouldn't an 
implementation feel free to create and discard as many instances as 
needed? Whether there is one INIT instance or a million is an 
implementation detail that shouldn't matter to the user, they should be 
able to just write "if state == INIT" and get the right answer.


> The implementation should be able to change the state objects to any
> (distinct) objects at all (say, the new enums, or ints, or some more
> elaborate class instances) without any changes in the code.

In practice, this is often not true. Often you have compatibility 
requirements with (say) external libraries or protocols, where the states 
are set by value. E.g. you may have to be compatible with a C library 
where the states are given as ints.

But more importantly, you don't actually have that much freedom to change 
the states. Common sense[1] tells us that we're not really free to 
replace states with arbitrary objects, they should be values that match 
the name of the state. Given a state BLUE, we want inspecting it in a 
debugger, or printing it, to display BLUE, not sBxvGe74sk or 23 or 
<object object at 0xb7c1a568> and certainly not YELLOW.

After all, the states are *symbols*, which means they don't really have 
any (internal) state or behaviour apart from their name. (In Python, we 
have to given them a value, we can't just refer to name BLUE without 
giving it a value, but the value isn't important. What we really care 
about is the name.

Fundamentally, there's not terribly much difference between an API that 
says:

    "The colour variable can be BLUE or YELLOW"

versus one that says:

    "The colour variable can be 'BLUE' or 'YELLOW'"


In both cases, you can't change the public API. Adding or subtracting a 
couple of quotation marks is not a big deal (although perhaps it is a 
small deal).

Although it may seem at first that by exposing the implementation 
("states are given by strings") you're limiting your freedom to change 
the implementation, in practice you don't actually have that much freedom 
to do so, and very likely you're never[2] going to use that freedom 
anyway. States are symbols, and rarely need behaviour apart from 
equality, so why would you bother to change the implementation?

(If this sounds like a mild argument against Enums, I guess it is. After 
all, Python worked quite well for 20+ years without Enums. Using strings 
as "poor man's enums" works well enough. That's why it took so long for 
Python to get "proper" enums, and even then, they aren't a core feature 
of the language.)




[1] Common sense: so rare it is practically a super power.

[2] Well, hardly ever.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#67214

FromChris Angelico <rosuav@gmail.com>
Date2014-02-28 22:08 +1100
Message-ID<mailman.7465.1393585687.18130.python-list@python.org>
In reply to#67212
On Fri, Feb 28, 2014 at 9:02 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> PS On the topic of enums, when are we getting support for a switch
> statement?

I don't see that they're particularly connected. In my C code, I've
used enums frequently as quick constants, often never switching on
them. Conversely, I often use switch in C code with either integer or
character constants. (ASCII character, of course, as that's all you
get.) Take this, for example, from my C++ MUD client:

enum {IS=0x00,ECHO=0x01,SEND=0x01,SUPPRESSGA=0x03,TERMTYPE=0x18,NAWS=0x1F,SE=0xF0,GA=0xF9,SB,WILL,WONT,DO=0xFD,DONT,IAC=0xFF};

That one happens to be used in a switch at one point, but it's also
used in other ways, like this:

static char naws[]={IAC,SB,NAWS,0,0,0,0,IAC,SE};

(The actual window size gets patched in separately, in case you're
curious... but I suspect most people here won't be aware that IAC SB
NAWS means Interpret-As-Command, Subnegotiation Begin, Negotiate About
Window Size, and that this is a TELNET command sequence.)

With bit flags, they'll never be used in switch:

enum //Bitflags in hackity
{
    HACK_ATAT=1, //Perform @@ -> fill-in translation
    HACK_AUTOEDIT=2, //Respond to the autoedit markers [now active by default]
};

...

if (hackity&HACK_ATAT) ...

Sometimes, they're just states, and they're assigned and/or compared
for equality:

enum {ic, court, citizen, trivia, sports, chancount} lastlinetype;

Some things check if (lastlinetype == ic), but nothing ever switches on it.

Meanwhile, here's a switch block from Gypsum that will never use enumerations:

switch (max(delay,base))
{
    case 0..59: ... handling for <1 minute ...
    case 60..3599: ... handling for <1 hour ...
    default: ... handling for >= 1 hour ...
}

No, the features are quite independent. Python currently has dispatch
tables and if/elif chains, and a strong cultural aversion to switch.
You could change that by coming up with some *really* awesome
proposal, but you'll be fighting against the tide a bit.

ChrisA

[toc] | [prev] | [next] | [standalone]


#67216

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-02-28 13:38 +0200
Message-ID<87y50vwjq4.fsf@elektro.pacujo.net>
In reply to#67214
Chris Angelico <rosuav@gmail.com>:

> Python currently has dispatch tables and if/elif chains, and a strong
> cultural aversion to switch. You could change that by coming up with
> some *really* awesome proposal, but you'll be fighting against the
> tide a bit.

It's easy have a "cultural aversion" when the language doesn't provide
the facility.

Switch statements provide for excellent readability in parsers and state
machines, for example. They also allow the Python compiler to optimize
the statement internally unlike long if-else chains.


Marko

[toc] | [prev] | [next] | [standalone]


#67218

FromChris Angelico <rosuav@gmail.com>
Date2014-02-28 23:22 +1100
Message-ID<mailman.7467.1393590134.18130.python-list@python.org>
In reply to#67216
On Fri, Feb 28, 2014 at 10:38 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> Python currently has dispatch tables and if/elif chains, and a strong
>> cultural aversion to switch. You could change that by coming up with
>> some *really* awesome proposal, but you'll be fighting against the
>> tide a bit.
>
> It's easy have a "cultural aversion" when the language doesn't provide
> the facility.

I'm talking about the strong resistance that gets put up any time the
suggestion comes up on python-ideas or somesuch. The core devs and
Guido especially are against the idea.

> Switch statements provide for excellent readability in parsers and state
> machines, for example. They also allow the Python compiler to optimize
> the statement internally unlike long if-else chains.

It's usually possible to turn any concrete example of a switch
statement into an equally-readable dispatch table. All you need is for
your search terms to be hashable (which is a much less stringent
requirement than is usually put on switch blocks, like "must be
machine word signed integer"), and you can do something like this:

compare_key = {
    # Same target(s).
    ast.Assign: lambda node: ' '.join(dump(t) for t in node.targets),
    # Same target and same operator.
    ast.AugAssign: lambda node: dump(node.target) + dump(node.op) + "=",
    # A return statement is always compatible with another.
    ast.Return: lambda node: "(easy)",
    # Calling these never compatible is wrong. Calling them
    # always compatible will give lots of false positives.
    ast.Expr: lambda node: "(maybe)",
    # These ones are never compatible, so return some
    # object that's never equal to anything.
    ast.Import: lambda node: float("nan"),
    ast.ImportFrom: lambda node: float("nan"),
    ast.Pass: lambda node: float("nan"),
    ast.Raise: lambda node: float("nan"),
    ast.If: lambda node: float("nan"),
}

I then effectively do a big switch block like this:

if try_type not in compare_key:
    print("Unrecognized type",try_type.__name__,file=sys.stderr)
    compare_key[try_type] = lambda node: float("nan")
func = compare_key[try_type]
try_node = func(node.body[0])
for handler in node.handlers:
    if try_node != func(handler.body[0]): return

The first check (the "not in" bit) is kinda like a default clause, but
it makes the output only once for any given type. For a more 'true'
default clause, I could do this:

try:
    func = compare_key[try_type]
except KeyError:
    func = compare_key["default"]

but I take advantage of the fact that the dispatch table is a dict and
mutate it. Also, if this were done in a switch block, there'd be some
redundancy. I call the same function on node.body[0] and on
handler.body[0] for each handler in handlers, so there's structure
that's common to all the branches there. I'm not sure how, with a
classic C-style switch block, I could implement that cleanly. Probably
I'd end up using function pointers and basically doing it exactly the
same way :)

The only major thing C's switch does that a dispatch table doesn't is
fall-through. And let's face it, if your big argument in favour of a
switch statement is "I need fall-through", you're not just going to
have Python devs against you, you're also going to fight against the
roughly 50% of C programmers who detest that feature :)

(FWIW, I'm in the other 50%. I quite like fall-through, and there are
times when it's a very clean way to express something. But even those
cases can usually be expressed one way or another with only a little
more redundancy - for instance, have one case that sets the key to be
the next one, and then have stand-alone if blocks rather than if/elif.
Considering that that use of fall-through usually requires an
explanatory comment anyway, you're not really losing much.)

So, going back to your statement:

> Switch statements provide for excellent readability in parsers and state
> machines, for example.

The best way to start trying to build support for this would be to
mock up a syntax for a switch statement, and find a currently-existing
parser or state machine to translate. Show us the "before and after"
shots. That's what I'm currently doing to justify an exception
expression syntax - examples like this:

    pwd = (os.getcwd() except OSError: None)

    # Lib/tkinter/filedialog.py:210:
    try:
        pwd = os.getcwd()
    except OSError:
        pwd = None


    g = (grp.getgrnam(tarinfo.gname)[2] except KeyError: tarinfo.gid)
    u = (pwd.getpwnam(tarinfo.uname)[2] except KeyError: tarinfo.uid)

    # Lib/tarfile.py:2198:
    try:
        g = grp.getgrnam(tarinfo.gname)[2]
    except KeyError:
        g = tarinfo.gid
    try:
        u = pwd.getpwnam(tarinfo.uname)[2]
    except KeyError:
        u = tarinfo.uid


This is real Python code, straight out of the standard library. I
don't know if you could find many examples in the stdlib that beg for
a switch statement, but pick up some real-world code of your own, or
from some open source project, or something. Even if, in practice, it
wouldn't be changed for many years (because that project needs to be
compatible with previous versions of Python), it'd be worth showing
"here's what could be".

And then be prepared for a few hundred posts' worth of bikeshedding
about the details :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#67220

FromNeil Cerutti <neilc@norwich.edu>
Date2014-02-28 13:47 +0000
Message-ID<mailman.7468.1393595250.18130.python-list@python.org>
In reply to#67216
On 2014-02-28, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>> Python currently has dispatch tables and if/elif chains, and a
>> strong cultural aversion to switch. You could change that by
>> coming up with some *really* awesome proposal, but you'll be
>> fighting against the tide a bit.
>
> It's easy have a "cultural aversion" when the language doesn't
> provide the facility.
>
> Switch statements provide for excellent readability in parsers
> and state machines, for example. They also allow the Python
> compiler to optimize the statement internally unlike long
> if-else chains.

Once you remove all the features of switch that wouldn't fit in
Python, e.g.; fall-through, jumps, breaks; whats left provides
negligible benefit over if-elif-else or dict-dispatch.

A pythonic switch statement doesn't provide enough features to
bother with its implemention.

Check out Go's switch statement for an example of what it might
look like in Python. Except you'd get it without labeled break or
the fallthrough statement. Would you still want to use it?

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


Page 5 of 11 — ← Prev page 1 … 3 4 [5] 6 7 … 11  Next page →

Back to top | Article view | comp.lang.python


csiph-web