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


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

Closures in leu of pointers?

Started bycts.private.yahoo@gmail.com
First post2013-06-29 02:34 -0700
Last post2013-06-30 11:18 -0600
Articles 20 on this page of 52 — 14 participants

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


Contents

  Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 02:34 -0700
    Re: Closures in leu of pointers? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-29 10:42 +0100
    Re: Closures in leu of pointers? Peter Otten <__peter__@web.de> - 2013-06-29 12:01 +0200
      Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 04:21 -0700
        Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 04:44 -0700
          Re: Closures in leu of pointers? Peter Otten <__peter__@web.de> - 2013-06-29 14:12 +0200
            Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 05:26 -0700
              Re: Closures in leu of pointers? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-29 15:37 +0100
                Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 07:48 -0700
          Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 07:56 -0600
          Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 12:36 -0600
        Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 08:02 -0600
        Re: Closures in leu of pointers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-29 19:02 +0200
          Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-29 10:33 -0700
            Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 11:37 -0700
              Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 12:58 -0600
              Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 18:59 +0000
          Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 18:51 +0000
            Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 13:04 -0600
            Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-29 12:11 -0700
        Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 12:35 -0600
          Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 19:19 +0000
            Re: Closures in leu of pointers? Tim Chase <tim@thechases.com> - 2013-06-29 14:42 -0500
              Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-30 01:41 +0000
            Re: Closures in leu of pointers? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-29 21:02 +0100
            Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 16:02 -0600
        Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 18:45 +0000
          Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 19:00 +0000
          Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 12:20 -0700
            Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 19:33 +0000
              Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 12:34 -0700
              Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 13:47 -0600
              Re: Closures in leu of pointers? Terry Reedy <tjreedy@udel.edu> - 2013-06-29 16:53 -0400
                Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-30 01:56 -0700
                  Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-30 11:22 +0000
                    Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-30 04:42 -0700
              Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 15:21 -0600
                Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 14:30 -0700
              Re: Closures in leu of pointers? Terry Reedy <tjreedy@udel.edu> - 2013-06-30 00:32 -0400
              Re: Closures in leu of pointers? Chris Angelico <rosuav@gmail.com> - 2013-06-30 15:08 +1000
                Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-30 00:36 -0700
              Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 23:46 -0600
                Re: Closures in leu of pointers? alex23 <wuwei23@gmail.com> - 2013-07-01 14:57 +1000
                Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-01 07:36 +0000
              Re: Closures in leu of pointers? Chris Angelico <rosuav@gmail.com> - 2013-06-30 15:59 +1000
              Re: Closures in leu of pointers? Terry Reedy <tjreedy@udel.edu> - 2013-06-30 02:11 -0400
              Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 00:24 -0600
            Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 16:03 -0600
        Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 13:23 -0600
        Re: Closures in leu of pointers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-30 12:07 +0200
        Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 11:13 -0600
        Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 11:18 -0600

Page 1 of 3  [1] 2 3  Next page →


#49408 — Closures in leu of pointers?

Fromcts.private.yahoo@gmail.com
Date2013-06-29 02:34 -0700
SubjectClosures in leu of pointers?
Message-ID<2a2072e3-4b12-4ada-872c-1240d2379928@googlegroups.com>
Hi,

I'd like to use closures to set allow a subroutine to set variables in its caller, in leu of pointers.  But I can't get it to work.  I have the following test pgm, but I can't understand its behaviour:

It uses a function p2() from the module modules.closure1b:

  def p2 (proc):
    proc ("dolly")

I thought the following worked like I expected it to:


from modules.closures1b import p2

def p1(msg1):
    msg3 = "world"
    print "p1: entered: ", msg1
    def p11(msg2):
        print "p11: entered: ", msg2
        print msg1 + msg2 + msg3
    print p2 (p11)

p1('hello')

$ python closures1c.py 
p1: entered:  hello
p11: entered:  dolly
hellodollyworld
None

In other words, p1() is passed "hello" for msg1, "world" goes to the local msg3 and then p11() is invoked out of a remote module and it can access not only its own argument (msg2) but also the variables local to p1(): "hellodollyworld".

But if I try to set the variable local to p1(), all of a sudden python seems to forget everything we agreed on. 

If I add this line to the script above:
        msg3 = "goodbye"
as follows:

from modules.closures1b import p2

def p1(msg1):
    msg3 = "world"
    print "p1: entered: ", msg1
    def p11(msg2):
        print "p11: entered: ", msg2
        print msg1 + msg2 + msg3
        msg3 = "goodbye"          # <- new
    print p2 (p11)

p1('hello')

then all of a sudden, I get this:

p1: entered:  hello
p11: entered:  dolly
Traceback (most recent call last):
  File "closures1c.py", line 13, in <module>
    p1('hello')
  File "closures1c.py", line 11, in p1
    print p2 (p11)
  File "/home/mellman/eg/python/modules/closures1b.py", line 2, in p2
    proc ("dolly")
  File "closures1c.py", line 9, in p11
    print msg1 + msg2 + msg3
UnboundLocalError: local variable 'msg3' referenced before assignment


Huh?  msg3 isn't more referenced than it was before!

Can anyone explain this to me?



[toc] | [next] | [standalone]


#49409

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-06-29 10:42 +0100
Message-ID<mailman.3983.1372499409.3114.python-list@python.org>
In reply to#49408

[Multipart message — attachments visible in raw view] — view raw

On 29 Jun 2013 10:38, <cts.private.yahoo@gmail.com> wrote:
>
> Hi,
>
> I'd like to use closures to set allow a subroutine to set variables in
its caller, in leu of pointers.  But I can't get it to work.  I have the
following test pgm, but I can't understand its behaviour:
>
> It uses a function p2() from the module modules.closure1b:
>
>   def p2 (proc):
>     proc ("dolly")
>
> I thought the following worked like I expected it to:
>
>
> from modules.closures1b import p2
>
> def p1(msg1):
>     msg3 = "world"
>     print "p1: entered: ", msg1
>     def p11(msg2):
>         print "p11: entered: ", msg2
>         print msg1 + msg2 + msg3
>     print p2 (p11)
>
> p1('hello')
>
> $ python closures1c.py
> p1: entered:  hello
> p11: entered:  dolly
> hellodollyworld
> None
>
> In other words, p1() is passed "hello" for msg1, "world" goes to the
local msg3 and then p11() is invoked out of a remote module and it can
access not only its own argument (msg2) but also the variables local to
p1(): "hellodollyworld".
>
> But if I try to set the variable local to p1(), all of a sudden python
seems to forget everything we agreed on.
>
> If I add this line to the script above:
>         msg3 = "goodbye"
> as follows:
>
> from modules.closures1b import p2
>
> def p1(msg1):
>     msg3 = "world"
>     print "p1: entered: ", msg1
>     def p11(msg2):
>         print "p11: entered: ", msg2
>         print msg1 + msg2 + msg3
>         msg3 = "goodbye"          # <- new
>     print p2 (p11)
>
> p1('hello')
>
> then all of a sudden, I get this:
>
> p1: entered:  hello
> p11: entered:  dolly
> Traceback (most recent call last):
>   File "closures1c.py", line 13, in <module>
>     p1('hello')
>   File "closures1c.py", line 11, in p1
>     print p2 (p11)
>   File "/home/mellman/eg/python/modules/closures1b.py", line 2, in p2
>     proc ("dolly")
>   File "closures1c.py", line 9, in p11
>     print msg1 + msg2 + msg3
> UnboundLocalError: local variable 'msg3' referenced before assignment
>
>
> Huh?  msg3 isn't more referenced than it was before!
>
> Can anyone explain this to me?

The fact that msg3 is assigned to in that scope makes it a local variable.
It doesn't matter if the assignment happens later. Python will treat it as
local, and so won't look for it outside the local scope/closure.

The fix is to declare msg3 as global, I think.

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


#49410

FromPeter Otten <__peter__@web.de>
Date2013-06-29 12:01 +0200
Message-ID<mailman.3984.1372500107.3114.python-list@python.org>
In reply to#49408
cts.private.yahoo@gmail.com wrote:

> I'd like to use closures to set allow a subroutine to set variables in its
> caller, in leu of pointers.

"leu"? Must be a Fench word ;)

> But I can't get it to work.  I have the
> following test pgm, but I can't understand its behaviour:
> 
> It uses a function p2() from the module modules.closure1b:
> 
>   def p2 (proc):
>     proc ("dolly")
> 
> I thought the following worked like I expected it to:
> 
> 
> from modules.closures1b import p2
> 
> def p1(msg1):
>     msg3 = "world"
>     print "p1: entered: ", msg1
>     def p11(msg2):
>         print "p11: entered: ", msg2
>         print msg1 + msg2 + msg3
>     print p2 (p11)
> 
> p1('hello')
> 
> $ python closures1c.py
> p1: entered:  hello
> p11: entered:  dolly
> hellodollyworld
> None
> 
> In other words, p1() is passed "hello" for msg1, "world" goes to the local
> msg3 and then p11() is invoked out of a remote module and it can access
> not only its own argument (msg2) but also the variables local to p1():
> "hellodollyworld".
> 
> But if I try to set the variable local to p1(), all of a sudden python
> seems to forget everything we agreed on.
> 
> If I add this line to the script above:
>         msg3 = "goodbye"
> as follows:
> 
> from modules.closures1b import p2
> 
> def p1(msg1):
>     msg3 = "world"
>     print "p1: entered: ", msg1
>     def p11(msg2):
>         print "p11: entered: ", msg2
>         print msg1 + msg2 + msg3
>         msg3 = "goodbye"          # <- new
>     print p2 (p11)
> 
> p1('hello')
> 
> then all of a sudden, I get this:
> 
> p1: entered:  hello
> p11: entered:  dolly
> Traceback (most recent call last):
>   File "closures1c.py", line 13, in <module>
>     p1('hello')
>   File "closures1c.py", line 11, in p1
>     print p2 (p11)
>   File "/home/mellman/eg/python/modules/closures1b.py", line 2, in p2
>     proc ("dolly")
>   File "closures1c.py", line 9, in p11
>     print msg1 + msg2 + msg3
> UnboundLocalError: local variable 'msg3' referenced before assignment
> 
> 
> Huh?  msg3 isn't more referenced than it was before!
> 
> Can anyone explain this to me?

You picked the most obnoxious variable names I can think of, but the actual 
problem is simple:

Python statically determines the scope of a variable. If you rebind a name 
it becomes a local variable unless you explicitly declare it as global or -- 
in Python 3 -- as nonlocal. For example:

Wrong:

>>> def outer():
...     n = 0
...     def inner():
...             print(n)
...             n += 1
...     return inner
... 
>>> outer()()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in inner
UnboundLocalError: local variable 'n' referenced before assignment

With nonlocal declaration (Python 3 only):
>>> def outer():
...     n = 0
...     def inner():
...             nonlocal n
...             print(n)
...             n += 1
...     return inner
... 
>>> f = outer()
>>> f()
0
>>> f()
1
>>> f()
2

With a mutable variable as a pseudo-namespace (workaround for Python 2):

>>> def outer():
...     n = [0]
...     def inner():
...             print n[0]
...             n[0] += 1
...     return inner
... 
>>> f = outer()
>>> f()
0
>>> f()
1

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


#49411

Fromcts.private.yahoo@gmail.com
Date2013-06-29 04:21 -0700
Message-ID<bc798729-f935-448d-9d62-c409b2d12e76@googlegroups.com>
In reply to#49410
Well, it would have been French if I had spelled it right - since you force me overcome my laziness, I see I should have spelled it lieu ...

Thank you.  You reminded me of the (weak) workaround of using arrays and confirmed my suspicion that I although I can read the variable, I won't be able to write to it.  I still don't understand why not, though...

As for python 3 ... "nonlocal"?  I see I'm not alone in picking obnoxious names ...

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


#49413

Fromcts.private.yahoo@gmail.com
Date2013-06-29 04:44 -0700
Message-ID<2603336f-e9d2-441a-8535-957d8fec2002@googlegroups.com>
In reply to#49411
Alas, one reason it's a weak workaround is that it doesn't work - at least, not how I wish it would:


$ cat ptrs

x = 34

def p1 (a1):

    a1[0] += 12
    
p1 ([x])

print (x)

$ python ptrs
34

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


#49414

FromPeter Otten <__peter__@web.de>
Date2013-06-29 14:12 +0200
Message-ID<mailman.3985.1372507979.3114.python-list@python.org>
In reply to#49413
cts.private.yahoo@gmail.com wrote:

> As for python 3 ... "nonlocal"?  I see I'm not alone in picking obnoxious
> names ...

tous chez...

> Alas, one reason it's a weak workaround is that it doesn't work - at
> least, not how I wish it would:

> $ cat ptrs
> 
> x = 34
> 
> def p1 (a1):
> 
>     a1[0] += 12
>     
> p1 ([x])
> 
> print (x)
> 
> $ python ptrs
> 34

That doesn't work with 'nonlocal' either. 

You can compare Python's names with (automatically dereferenced and 
garbage-collected) pointers, but there is no way to have a name refer to 
another name. The C-like

f(var *value)
{
    value = ...
}

is the only way to pass a variable in Python. There is no way to emulate

f(var **value)
{
   *value = ...
}

When this came up a few days ago I linked to

http://learntofish.wordpress.com/2012/01/09/call-by-object-reference-call-by-sharing/

but I was actually looking for the classic, so there:

http://python.net/~mwh/hacks/objectthink.html

PS: If you're reading this and love the French language -- I am deeply sorry 
for the pain I'm causing you...

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


#49415

Fromcts.private.yahoo@gmail.com
Date2013-06-29 05:26 -0700
Message-ID<3cbe7828-bd95-42d9-b263-6e6c048cace6@googlegroups.com>
In reply to#49414
"PS: If you're reading this and love the French language -- I am deeply sorry
for the pain I'm causing you..."

It's obviously a team effort...

My French ain't so hot, either.  I had to google your "tout chez" until I ran into the explanation:

  hallo :) also ich gucke super gerne two and a half men und da wird öfters tout chez (keine ahnung ob es so geschrieben wird) gesagt. ich hab gegooglet aber nichts gefunden. ich habs auch überstzen lassen aber da kommt nur raus "alles bei"...das wirds ja wohl nicht sein^^ könnte mir also jemand sagen was es genau bedeutet wenn man das sagt?

The answer for us TV-challenged non-views:

  Es heißt: "touché"

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


#49425

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-06-29 15:37 +0100
Message-ID<mailman.3993.1372516629.3114.python-list@python.org>
In reply to#49415
On 29/06/2013 13:26, cts.private.yahoo@gmail.com wrote:
> "PS: If you're reading this and love the French language -- I am deeply sorry
> for the pain I'm causing you..."
>
> It's obviously a team effort...
>
> My French ain't so hot, either.  I had to google your "tout chez" until I ran into the explanation:
>
>    hallo :) also ich gucke super gerne two and a half men und da wird öfters tout chez (keine ahnung ob es so geschrieben wird) gesagt. ich hab gegooglet aber nichts gefunden. ich habs auch überstzen lassen aber da kommt nur raus "alles bei"...das wirds ja wohl nicht sein^^ könnte mir also jemand sagen was es genau bedeutet wenn man das sagt?
>
> The answer for us TV-challenged non-views:
>
>    Es heißt: "touché"
>

Try reading Stephen Clarke's "1000 Years of Annoying the French". 
Perfect when summing up how good they are are at raising white flags 
wherever and whenever it suits them.

-- 
"Steve is going for the pink ball - and for those of you who are 
watching in black and white, the pink is next to the green." Snooker 
commentator 'Whispering' Ted Lowe.

Mark Lawrence

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


#49426

Fromcts.private.yahoo@gmail.com
Date2013-06-29 07:48 -0700
Message-ID<b607ee79-b3fa-4e46-ac67-5e488d4255e3@googlegroups.com>
In reply to#49425
I love the title.  Reminds me of Ivanhoe ... great time travel.

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


#49419

FromMichael Torrie <torriem@gmail.com>
Date2013-06-29 07:56 -0600
Message-ID<mailman.3988.1372514206.3114.python-list@python.org>
In reply to#49413
On 06/29/2013 05:44 AM, cts.private.yahoo@gmail.com wrote:
> Alas, one reason it's a weak workaround is that it doesn't work - at least, not how I wish it would:
> 
> 
> $ cat ptrs
> 
> x = 34
> 
> def p1 (a1):
> 
>     a1[0] += 12
>     
> p1 ([x])
> 
> print (x)
> 
> $ python ptrs
> 34

you'll have to use it more like this (and also changing your names to be
a bit more sane):

x = [ 34, ]

def test_func( out ):
    out[0] += 12

test_func(x)

print (x)

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


#49440

FromMichael Torrie <torriem@gmail.com>
Date2013-06-29 12:36 -0600
Message-ID<mailman.4002.1372531006.3114.python-list@python.org>
In reply to#49413
On 06/29/2013 07:56 AM, Michael Torrie wrote:
> x = [ 34, ]
> 
> def test_func( out ):
>     out[0] += 12
> 
> test_func(x)
> 
> print (x)

Well, actually
  print (x[0])

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


#49420

FromMichael Torrie <torriem@gmail.com>
Date2013-06-29 08:02 -0600
Message-ID<mailman.3989.1372514543.3114.python-list@python.org>
In reply to#49411
On 06/29/2013 05:21 AM, cts.private.yahoo@gmail.com wrote:
> Thank you.  You reminded me of the (weak) workaround of using arrays
> and confirmed my suspicion that I although I can read the variable, I
> won't be able to write to it.  I still don't understand why not,
> though...

The real problem here is that you don't understand how python variables
work.  And in fact, python does not have variables.  It has names that
bind to objects.  If you assign a value to a name, you're not
overwriting a variable, you're rebinding a name to a new object in the
default scope (unless it's declared global, or nonlocal in python 3, the
latter I assume uses the same scope in which it was first declared, but
I could be wrong).  Since the name is in the local scope, the original
name in the caller's scope is still bound to its original object.

Furthermore, most primitive objects in python (strings, ints, etc) are
immutable, which means they can *never change*.  "Writing" to a variable
simply dereferences the old object (which may still be referenced in the
caller's scope) and creates a new object and binds it to the name.

A list is mutable, which is why it's one solution to emulate a variable.

> As for python 3 ... "nonlocal"?  I see I'm not alone in picking
> obnoxious names ...

nonlocal at least has meaning.

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


#49435

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-06-29 19:02 +0200
Message-ID<mailman.4000.1372525392.3114.python-list@python.org>
In reply to#49411
Op 29-06-13 16:02, Michael Torrie schreef:
>
> The real problem here is that you don't understand how python variables
> work.  And in fact, python does not have variables.  It has names that
> bind to objects.

I don't understand why members of this list keep saying this. Sure the
variables in python behave differently than those in C and algol  But 
they behave similarly as those in smalltalk and lisp and I haven't seen
anyone claim that smalltalk and lisp don't have variables.

We might as well say that C doesn't have variables, it has names
pointing to memory locations or value containers or something
like that.

AFAICS there is no reason why "variable" wouldn't be appropiate
for python names as opposed to C names.

-- 
Antoon Pardon

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


#49437

Fromrusi <rustompmody@gmail.com>
Date2013-06-29 10:33 -0700
Message-ID<23aa9a24-54ae-4758-a635-98658fd82633@googlegroups.com>
In reply to#49435
On Saturday, June 29, 2013 10:32:01 PM UTC+5:30, Antoon Pardon wrote:
> Op 29-06-13 16:02, Michael Torrie schreef:
> > The real problem here is that you don't understand how python variables
> > work.  And in fact, python does not have variables.  It has names that
> > bind to objects.
> 
> I don't understand why members of this list keep saying this. Sure the
> variables in python behave differently than those in C and algol  But 
> they behave similarly as those in smalltalk and lisp and I haven't seen
> anyone claim that smalltalk and lisp don't have variables.
> 
> We might as well say that C doesn't have variables, it has names
> pointing to memory locations or value containers or something
> like that.
> 
> AFAICS there is no reason why "variable" wouldn't be appropiate
> for python names as opposed to C names.

Well mathematicians (or to be more precise functional programmers pretending to be mathematicians) claim that any imperative language does not have variables.

And recently on this list I saw the exact opposite claim -- functional languages dont have variables.

I also remember my statistics teacher dinning it into us -- a random variable is not a variable.

So each one varies according to his own notions I guess :-)

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


#49441

Fromcts.private.yahoo@gmail.com
Date2013-06-29 11:37 -0700
Message-ID<81f3ee82-eb33-4dfe-9f25-8fa7e9669088@googlegroups.com>
In reply to#49437
:)  Thank you guys for saying what I was biting my tongue about (thanks everybody for the help, BTW!).

This "python-think" stuff was starting to get on my nerves - but then it occurred to me that - although having many powerful features - it has so many weird restrictions that it requires a special way of thinking and problem solving.

I have to work with perl's object-orientation stuff again for awhile, in order to see if either has an advantage.

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


#49444

FromMichael Torrie <torriem@gmail.com>
Date2013-06-29 12:58 -0600
Message-ID<mailman.4003.1372532321.3114.python-list@python.org>
In reply to#49441
On 06/29/2013 12:37 PM, cts.private.yahoo@gmail.com wrote:
> :)  Thank you guys for saying what I was biting my tongue about
> (thanks everybody for the help, BTW!).

Sometimes it's best to state the actual problem you're trying to solve
and see if there's a pythonic solution that fits it rather than to hack
a solution transliterated from C.  I'm curious as to if you did get
something working and what you ended up with.

> This "python-think" stuff was starting to get on my nerves - but then
> it occurred to me that - although having many powerful features - it
> has so many weird restrictions that it requires a special way of
> thinking and problem solving.

Interesting point of view.  "Pythonic" ways of programming is in my mind
the number one appeal of Python.  It's quite clean yet practical.  Has
enough of the intellectual purity of LISP, Smalltalk, and functional
languages to be appealing, yet the practicality of a traditional
procedural language.

In any language, though you have to grasp the data model.  Usually the
criticisms of Python come from not a failure to do this, either because
it's hard to learn at first, or because people dislike learning
something different than what they are used to.  A while back we had a
fairly pleasant gentleman come on the list from a C# background.  His
frustrations with Python stemmed from wanting it to be like C#, which of
course it isn't.  He did not have much success and I'm afraid was left
with a sour taste of Python, which of course had nothing to do with the
language itself.  Python certainly has inconsistencies and there are
newbie behavioral gotchas.

> I have to work with perl's object-orientation stuff again for awhile,
> in order to see if either has an advantage.

Your original post mentioned nothing about object-orientation, so I have
no idea how you intend to use OO design, but I think you'll find
Python's model fairly workable and consistent.

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


#49445

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-29 18:59 +0000
Message-ID<51cf2e77$0$29999$c3e8da3$5496439d@news.astraweb.com>
In reply to#49441
On Sat, 29 Jun 2013 11:37:55 -0700, cts.private.yahoo wrote:

> :)  Thank you guys for saying what I was biting my tongue about (thanks
> everybody for the help, BTW!).
> 
> This "python-think" stuff was starting to get on my nerves - but then it
> occurred to me that - although having many powerful features - it has so
> many weird restrictions that it requires a special way of thinking and
> problem solving.

Er, no, you're confused. Python's restrictions aren't "weird", they make 
absolutely perfect sense once you understand the programming model. 
Python has a nice, clean semantic model, far cleaner than most other 
languages I've looked at, which *cannot decide* on what model they wish 
to present so they end up with a hodge-podge of bits of different models 
all lumped together and every piece of random syntax you could ever hope 
(or perhaps fear) to see.

If you insist in thinking of Python as "Perl with different syntax", then 
you won't understand why you can't do certain things -- and you'll 
equally not realise that you CAN do other things that other languages 
don't support.


-- 
Steven

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


#49443

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-29 18:51 +0000
Message-ID<51cf2cb7$0$29999$c3e8da3$5496439d@news.astraweb.com>
In reply to#49435
On Sat, 29 Jun 2013 19:02:01 +0200, Antoon Pardon wrote:

> Op 29-06-13 16:02, Michael Torrie schreef:
>>
>> The real problem here is that you don't understand how python variables
>> work.  And in fact, python does not have variables.  It has names that
>> bind to objects.
> 
> I don't understand why members of this list keep saying this. Sure the
> variables in python behave differently than those in C and algol  But
> they behave similarly as those in smalltalk and lisp and I haven't seen
> anyone claim that smalltalk and lisp don't have variables.
> 
> We might as well say that C doesn't have variables, it has names
> pointing to memory locations or value containers or something like that.
> 
> AFAICS there is no reason why "variable" wouldn't be appropiate for
> python names as opposed to C names.

You are absolutely correct in principle. But in practice, there are ten 
bazillion C, Pascal, COBOL, and BASIC programmers who understand the word 
"variable" to mean a named memory location, for every Smalltalk or Lisp 
programmer who understands a "variable" as a name binding. So it's pure 
weight of numbers thing.

The average Lisp programmer will be completely aware that "variable" can 
mean various things, and take care to determine what the word means in 
Python. She will immediately grok what we mean, even if she thinks that 
the "no variables" part is just an affectation ("Heh, those wacky Python 
dudes think they don't have variables!") but at least she'll understand 
the name binding part.

On the other hand, the average C programmer is barely aware that there 
are other languages at all, let alone that some of them differ from C in 
semantics as well as syntax. So by emphasising the differences ("Python 
has no variables? It has name bindings?") we increase the likelihood that 
he'll learn the differences in semantics as well as syntax.

So, in a very practical sense, "Python has no variables, it has name 
bindings" is completely wrong except in the sense that really matters: 
Python's variables don't behave identically to C variables.


-- 
Steven

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


#49447

FromMichael Torrie <torriem@gmail.com>
Date2013-06-29 13:04 -0600
Message-ID<mailman.4004.1372532699.3114.python-list@python.org>
In reply to#49443
On 06/29/2013 12:51 PM, Steven D'Aprano wrote:
> You are absolutely correct in principle. But in practice, there are ten 
> bazillion C, Pascal, COBOL, and BASIC programmers who understand the word 
> "variable" to mean a named memory location, for every Smalltalk or Lisp 
> programmer who understands a "variable" as a name binding. So it's pure 
> weight of numbers thing.
> 
> The average Lisp programmer will be completely aware that "variable" can 
> mean various things, and take care to determine what the word means in 
> Python. She will immediately grok what we mean, even if she thinks that 
> the "no variables" part is just an affectation ("Heh, those wacky Python 
> dudes think they don't have variables!") but at least she'll understand 
> the name binding part.
> 
> On the other hand, the average C programmer is barely aware that there 
> are other languages at all, let alone that some of them differ from C in 
> semantics as well as syntax. So by emphasising the differences ("Python 
> has no variables? It has name bindings?") we increase the likelihood that 
> he'll learn the differences in semantics as well as syntax.
> 
> So, in a very practical sense, "Python has no variables, it has name 
> bindings" is completely wrong except in the sense that really matters: 
> Python's variables don't behave identically to C variables.

Very good points.  Thank you.  Good tips for how to better explain
things next time it comes up.  I'll avoid simply saying "Python has no
variables."

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


#49448

Fromrusi <rustompmody@gmail.com>
Date2013-06-29 12:11 -0700
Message-ID<c506580f-f083-4a53-9528-ea7fef6df51c@googlegroups.com>
In reply to#49443
On Sunday, June 30, 2013 12:21:35 AM UTC+5:30, Steven D'Aprano wrote:
> On Sat, 29 Jun 2013 19:02:01 +0200, Antoon Pardon wrote:
> > We might as well say that C doesn't have variables, it has names
> > pointing to memory locations or value containers or something like that.
> > 
> > AFAICS there is no reason why "variable" wouldn't be appropiate for
> > python names as opposed to C names.
> 
> You are absolutely correct in principle. But in practice, there are ten 
> bazillion C, Pascal, COBOL, and BASIC programmers who understand the word 
> "variable" to mean a named memory location, for every Smalltalk or Lisp 
> programmer who understands a "variable" as a name binding. So it's pure 
> weight of numbers thing.

I note that the 10 bazillion C… programmers take precedence over 10 centuries of mathematics. I wonder if you are familiar with Weizenbaum -- early doyen of AI.  Here is a conversation whose first few paras are relevant
http://tech.mit.edu/V105/N16/weisen.16n.html

On a more technical note... that imperative programming is a bad idea is one of the harder-to-learn lessons for computer scientists
http://blog.languager.org/2012/11/imperative-programming-lessons-not.html

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


Page 1 of 3  [1] 2 3  Next page →

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


csiph-web