I'm trying to backport my Python3 package to Python2. I used pasteurize
and all works fine. I noticed that it imported a few stuff from builtins
including str
, int
and super
. I was wondering if importing everything from builtins
is safe. Yes, I know that in principle asterisk import is considered bad practice because it clutters the current namespace, doesn't make it clear what's imported and override names you don't intend to. But when it comes to builtins
in particular isn't it true that all of them are already present as names, are safe to import and shouldn't break anything?
Also, if using super
from builtins
in Python2 code is it safe to call it as Python3's super
with no arguments? Are there edge-cases where it may break with Python2?
from builtins import * # is this frowned upon?
from future import standard_library
standard_library.install_aliases()
class Foo(object):
def __init__(self):
super().__init__() # is this always safe in python 2?
Edit 1: Just to clarify, for Python2 builtins
comes from future
and is not a built-in module as in Python3.
Edit 2: Some people have suggested that calling super
with no arguments never works and that importing from builtins
makes no difference. Obviously this is wrong.
from __future__ import print_function
class Foo(object):
def __init__(self, x):
self.x = x
class Bar(object):
def __init__(self, x):
self.x = x + 1
class Baz(Bar, Foo):
def __init__(self, x):
super().__init__(x)
try:
b = Baz(1)
except TypeError as e:
# this will only happen in Python 2
print("Didn't work: {}; trying with builtins.super".format(str(e)))
from builtins import super
b = Baz(1) # this now works in Python 2.7 too
print(b.x)