6

I find myself mixing both import x and from x import y forms for the same module, depending how often a particular object is used and whether it is clear which modules it comes from.

For example I might write the following:

import dataclasses from dataclasses import dataclass import json @dataclass class Point3D: x: int y: int z: int def to_json(self) -> str: return json.dumps(dataclasses.as_dict(self)) 

I want json.dumps and dataclasses.as_dict to be called with their module name to avoid ambiguity e.g. with pickle.dumps and sympy.as_dict. But @dataclass clearly comes from dataclasses, making @dataclasses.dataclass redundant.

Is there a way to write:

import dataclasses from dataclasses import dataclass 

more cleanly, on a single line (semicolon doesn't count)? I would imagine something like from dataclasses import self, dataclass.

7
  • 6
    The short answer is: no, per docs.python.org/3/reference/…. What you already have is correct. Commented Jul 28 at 11:02
  • 6
    Note, for downvotes and close vote: I don't think this is opinion based. It is not as they asked "would it be nice if there was", or "it is needed", ... Just a pure fact-based question on python syntax. Whose pure, fact-based answer, "no" was given by johnsharpe. Answer is yes or no (and I also think it is "no"). Not "I like it or not". So, I don't get why the question should be closed (for this reason at least). Commented Jul 28 at 11:15
  • 1
    This question is similar to: Use 'import module' or 'from module import'?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 28 at 11:16
  • 5
    @BendingRodriguez: no, it is clearly not the same question. I would say that OP very probably already knows the answer to that question (or, more acurately, very probably has already an opinion about that question, since that old, unclosed and very upvoted question contrarily to the current one, is opinion-based). Commented Jul 28 at 11:18
  • 4
    @BendingRodriguez I think OP explained quite clearly why they actually want to do what they want to do. Whether that "makes no sense" is entirely irrelevant for the gist of the question. Commented Jul 28 at 11:25

1 Answer 1

5

The short answer, as given in jonrsharpe's comment:

no, per The import statement - Python Language Reference. What you already have is correct.

There is a bit of a workaround though:

Step 1: Provide a module, in which you organize your imports (call it e.g. my_imports.py):

# Contents of `my_imports.py` import dataclasses from dataclasses import dataclass import json 

Step 2: Use from my_imports import …:

from my_imports import dataclasses, dataclass, json @dataclass class Point3D: x: int y: int z: int def to_json(self) -> str: return json.dumps(dataclasses.asdict(self)) print(Point3D(x=1, y=2, z=3).to_json()) 

I cannot judge whether that makes sense for you or whether this appears too contrived. A potential use case that I could think of would be having very explicit control over what imports are allowed in a project: Provide all of them via my_imports in a single place; then, in all other modules, ensure that only my_imports is imported.

Two side notes: First, @dataclass is probably not as unambiguous as you might think; Pydantic, for example, provides an alternative implementation. Second, in dataclasses, contrary to SymPy, it's asdict(), not as_dict().

Finally, as an afterthought: I guess the described workaround is somewhat similar to what is called "barrel files" in JavaScript (I was not aware of these, since I am not really familiar with JavaScript). Googling also revealed, however, that barrel files seem to be a somewhat controversial concept, the first few hits containing phrases such as The Benefits and Disadvantages of Using Barrel Files and Please Stop Using Barrel Files. The latter finding makes me want to emphasize: I just wanted to show a possible workaround, I am not sure if it is really meaningful in any scenario in general (including the one that I mentioned) or in your use case in particular.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.