[Python-de] Python3 und "file"-Objekte

Christian Heimes christian at python.org
Fr Feb 22 15:10:14 CET 2013


Am 22.02.2013 15:01, schrieb Olе Streicher:
> Hallo,
> 
> ich möchte meinen Code gerne Python3-fähig machen. Da findet sich eine
> Stelle
> 
> def load(source):
>     if isinstance(source, file):
>         source = source.read()
>     # ...
> 
> wie kann ich das so umschreiben, dass es in Python2 und Python3
> funktioniert?

Mach keinen Typecheck sondern prüfe auf das Interface:

if hasattr(source, "read"):
    ...

Christian






Mehr Informationen über die Mailingliste python-de