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.
Calab and Larz60+ like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ImportError: cannot import name 'NullHandler' from 'logging' sukanta80 0 630 Jul-28-2025, 03:26 PM
Last Post: sukanta80
  Dynamic Module Import Error DaddyMAN 3 1,641 Jun-20-2025, 12:07 AM
Last Post: Pedroski55
  ImportError: cannot import name 'Pyfhel' from 'Pyfhel' Anldra12 11 8,145 Jun-16-2025, 07:08 AM
Last Post: ItsTheGoose
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 10 26,950 May-26-2025, 01:55 PM
Last Post: deanhystad
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 1,218 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  beginner doesn't understand import cimerio 3 1,164 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 1,352 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  python 3.13 : import whois HansieB 1 1,408 Nov-30-2024, 02:58 PM
Last Post: snippsat
Question [SOLVED] Why "import foo" + "from foo import bar"? Winfried 4 2,231 Nov-26-2024, 08:13 AM
Last Post: Winfried
  import data (.csv) into Jupyter notebook oranstprotonme 2 2,160 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme

Forum Jump:

User Panel Messages

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