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


Groups > de.comp.lang.python > #5142

Re: [Python-de] Migration Python 2 auf 3

From Stefan Behnel <python-de@behnel.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] Migration Python 2 auf 3
Date 2018-03-16 15:46 +0100
Message-ID <mailman.109.1521211619.1867.python-de@python.org> (permalink)
References <p8ggsi$96i$1@solani.org> <p8gkmm$bn2$2@solani.org> <8a7cb3d8-0784-c53c-0196-973192dc7951@behnel.de>

Show all headers | View raw


Philipp Klaus Krause schrieb am 16.03.2018 um 15:35:
> Der Rat,
> from __future__ import print_function
> zu verwenden, hat mir schonmal weitergeholfen. Vielen Dank.
> 
> Nun hätte ich noch ein
> 
>     m = re.match(r'([^/]*)/([^/]*)/([^/]*)/(.*)$', name)
>     if (m >= 3):
>         base = m.group(3)
> 
> das mit Python 2 funktioniert, aber mit Python 3 nicht:
> 
> Traceback (most recent call last):
>   File "./compact-results.py", line 50, in <module>
>     if (m >= 3):
> TypeError: '>=' not supported between instances of '_sre.SRE_Match' and
> 'int'

Sehr hübsch. Das ist ein Bug im Code, hätte nie da stehen dürfen,
funktioniert aber durch Zufall:

  Python 2.7.12 (default, Dec  4 2017, 14:50:18)
  [GCC 5.4.0 20160609] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import re
  >>> m = re.match('.', "abc")
  >>> m
  <_sre.SRE_Match object at 0x7fe76e8546b0>
  >>> m >= 3
  True
  >>> m = None
  >>> m >= 3
  False

Sprich: was du eigentlich willst, ist der Test: regex hat gematcht oder
nicht. Das machst du am einfachsten mit

    if m:
        ...

Da es in der regex immer vier Gruppen gibt, war die vermutliche
ursprüngliche Intention, auf die Anzahl der Gruppen zu testen, wohl auch
eher sinnfrei.

Stefan

Back to de.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Migration Python 2 auf 3 Philipp Klaus Krause <pkk@spth.de> - 2018-03-16 14:29 +0100
  Re: [Python-de] Migration Python 2 auf 3 Christopher Arndt <chris@chrisarndt.de> - 2018-03-16 14:44 +0100
  Re: [Python-de] Migration Python 2 auf 3 Hartmut Goebel <h.goebel@crazy-compilers.com> - 2018-03-16 14:44 +0100
    Re: [Python-de] Migration Python 2 auf 3 Philipp Klaus Krause <pkk@spth.de> - 2018-03-16 15:32 +0100
  Re: Migration Python 2 auf 3 Philipp Klaus Krause <pkk@spth.de> - 2018-03-16 15:35 +0100
    Re: [Python-de] Migration Python 2 auf 3 Stefan Behnel <python-de@behnel.de> - 2018-03-16 15:46 +0100
    Re: [Python-de] Migration Python 2 auf 3 Christopher Arndt <chris@chrisarndt.de> - 2018-03-16 15:50 +0100
    Re: [Python-de] Migration Python 2 auf 3 Stefan Behnel <python-de@behnel.de> - 2018-03-16 17:05 +0100

csiph-web