Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import vs from x import
#1
Question 
I'm trying to figure out why you would create a containing class if you were just going to import it from a file, vs just creating the general classes that you will simply import.

I'm just wondering if the two code examples below are equivilent. If not, what differences would there be?

Using simple "import Creds":

Creds.py:
import random

class Server1:
    url = "server1.com"
    username = "username"
    password = "password"
    randkey = random.randint(1, 10)

class Server2:
    url = "server2.com"
    username = "username"
    password = "password"
    randkey = random.randint(1, 10)
import Creds

print(Creds.Server2.randkey)
versus using "from creds import Creds":

creds.py:
import random

class Creds:

    class Server1:
        url = "server1.com"
        username = "username"
        password = "password"
        randkey = random.randint(1, 10)
        
    
    class Server2:
        url = "server2.com"
        username = "username"
        password = "password"
        randkey = random.randint(1, 10)
from creds import Creds

print(Creds.Server2.randkey)
Reply
#2
# Creds.py
class Server1: …  
class Server2: …
Here, Creds is a module whose attributes happen to be two classes. You do
import Creds
print(Creds.Server2.randkey)
Modules are singletons in sys.modules,they have their own namespace,and you can add functions, variables, etc. at the same level as Server1/Server2.

Nested classes inside a container class
# creds.py
class Creds:
    class Server1: …
    class Server2: …
Here, creds is still a module, but the only top-level symbol you expose is the Creds class. You do
from creds import Creds
print(Creds.Server2.randkey)
It’s more common in Python to use modules as namespaces.
If you need to break things out further, you’d typically make creds/ a package with __init__.py or separate sub-modules (creds/server1.py, etc.),
rather than nest classes which just add a level of confusing.
Nested classes are often reserved for cases where the inner class is really an implementation detail of the outer class (e.g. helper types), not just for grouping constants.

Tools (IDEs, linters, documentation generators) expect modules to contain top-level classes and functions.
Nesting can sometimes confuse auto-completion or doc-builders or people who try to read the code.

Unless you have a compelling reason to nest classes,
stick with the module-level approach (or split into sub-modules) for clarity, simplicity,and compatibility with the wider Python ecosystem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 798 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  beginner doesn't understand import cimerio 3 592 Feb-12-2025, 05:02 PM
Last Post: cimerio
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 711 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  python 3.13 : import whois HansieB 1 739 Nov-30-2024, 02:58 PM
Last Post: snippsat
Question [SOLVED] Why "import foo" + "from foo import bar"? Winfried 4 1,366 Nov-26-2024, 08:13 AM
Last Post: Winfried
  import data (.csv) into Jupyter notebook oranstprotonme 2 1,331 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme
  'import requests' problem Curbie 2 891 Aug-14-2024, 01:04 AM
Last Post: Curbie
  import a function from another file using relative path paul18fr 6 3,598 Aug-01-2024, 06:40 AM
Last Post: paul18fr
Bug Python 3.12 cannot import local python files as modules sunflowerdog 25 22,926 Jul-31-2024, 01:52 PM
Last Post: sunflowerdog
  ImportError: cannot import name 'scipy_namespace_for' from 'scipy._lib._array_api' AgileAVS 0 2,027 Jul-15-2024, 08:45 AM
Last Post: AgileAVS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020