Nested imports vs. two separate import statements?

  softwareengineering

Consider these two styles of code that accomplish the same thing:

Nested imports

common.models

from django.db.models import *

class SmallAutoField(fields.AutoField):
    def db_type(self, connection):
        return "smallserial"

myapp.models:

from common import models

class Test(models.Model):
    id = models.SmallAutoField()
    test = models.IntegerField()

Separate imports

common.models

from django.db import models

class SmallAutoField(models.fields.AutoField):
    def db_type(self, connection):
        return "smallserial"

myapp.models:

from django.db import models
from common import models as my_models

class Test(models.Model):
    id = my_models.SmallAutoField()
    test = models.IntegerField()

I know which version I prefer stylistically, but which version is the proper way to do it and why?

As for performance: I tried to timeit the Nested version, but that’s not possible because SyntaxError: import * only allowed at module level. So, is there another way to benchmark this method?

Bytecode for reference, but doesn’t look like it tells much:

>>> dis.dis('from django.db.models import *')
  1           0 LOAD_CONST               0 (0)
              3 LOAD_CONST               1 (('*',))
              6 IMPORT_NAME              0 (django.db.models)
              9 IMPORT_STAR
             10 LOAD_CONST               2 (None)
             13 RETURN_VALUE
>>> dis.dis('from django.db import models')
  1           0 LOAD_CONST               0 (0)
              3 LOAD_CONST               1 (('models',))
              6 IMPORT_NAME              0 (django.db)
              9 IMPORT_FROM              1 (models)
             12 STORE_NAME               1 (models)
             15 POP_TOP
             16 LOAD_CONST               2 (None)
             19 RETURN_VALUE

5

The class SmallAutoField is actually an innovation created for this particular application – it’s an improvement. It “moves the ball down the field” so to speak, upgrading the ability to describe persistence.

For a new reader of the program years from now (e.g. an engineer who needs to understand what it does) the intent of the second style is slightly clearer overall: they can easily see that “models” refers to to Django.db.models and “my_models” to something new.

The second style, highlights the credit awarded for the innovation by calling it “my_models.” It might be good to highlight the intent further like “serial_id_models” or “advanced_models.”

HTH — Rich

LEAVE A COMMENT