Martin v. Löwis wrote:
Mike Krell wrote:
    Well, 3to2 would then be an option for you: use Python 3 as the source
    language.
Making life easier for 3to2 is an *excellent* rationale for backports.


I'm skeptical. If new features get added to 2.7: why would that simplify
3to2? It couldn't *use* these features, since it surely would have to
support 2.6 and earlier as well.

Not sure what 3to2 would do about difficult-to-convert 3.x feature (as,
perhaps, the nonlocal keyword). If it currently gives up, it then may
offer you to restrict your target versions to 2.7+. Not sure whether
users would use that option, though - perhaps they rather stop using
nonlocal in 3.x if 3to2 cannot support it for all 2.x versions they are
interested in.

I would have thought you could translate nonlocal with the following:

Python 3:

def scope():
   name = value
   do_something_with(name)
   def inner():
       nonlocal name
       name = new_value
       do_something_else(name)

Python 2

def scope():
   name = [value]
   do_something_with(name[0])
   def inner():
       name[0] = new_value
       do_something_else(name[0])

I would love to see nonlocal backported to 2.7 as it cleans up a simple pattern that I use relatively often for testing.


Suppose you have an class and you want to test that method a calls method b, in Python 2 you might write something like this:

def test_method_a_calls_method_b():
  instance = SomeClass()
  was_called = []
  def method_b():
     was_called.append(True)

  instance.method_b = method_b
  instance.method_a()

  assert was_called == [True]

in Python 3 you can replace this with the slightly nicer:


def test_method_a_calls_method_b():
  instance = SomeClass()
  was_called = False
  def method_b():
     nonlocal was_called
     was_called = True

  instance.method_b = method_b
  instance.method_a()

  assert was_called

As to the argument that releasing 2.7 is pointless as few people would use it for several years, the success of Python 2.6 shows that although *many* people don't / can't use new versions of Python for several years many other people are able to and do use new versions of Python.

All the best,

Michael Foord

Perhaps 3to2 has a work-around that still provides a good backport in
most cases. Then, the backport would not make the tool any simpler: if
3to2 would start using the backport, it would actually get more
complicated (not easier), as it now needs to support two cases.

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk


--
http://www.ironpythoninaction.com/

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to