0

For instance, in Java, when we import some class we can access the content of the class without having to write the file name. Something like this:

import structures.Node;

public class JavaExample {
//then I can call Node directly
Node node = new Node();
}

But in Python, when I want to do the same thing I have to do this:

import node

class PythonExample:
    # have to write node. first
    my_node = node.Node()

Is there a way to call the Node class without the 'node.', like we do in Java?

flpn
  • 1,868
  • 2
  • 19
  • 31

3 Answers3

3

If you're going to be using something from that module frequently then you can import it specifically from the module and then reference it without the name:

from random import randint 
randint(1,10)

Or you can shorten the module name for convenience:

import random as r
r.randint(1,10)
Chase G.
  • 113
  • 5
1

In Python, you'll conventionally use the 'node.' but it's often shortened to save keystrokes. For example:

import pandas as pd
import numpy as np
1

The methods described by Chase G. and Jessica are generally what you want to do. For completeness' sake, the direct analog to what you're doing in Java would be from node import *. This is generally considered to be bad form due to namespace pollution among other things(1)(2)

(1): Why is "import *" bad?
(2): What exactly does "import *" import?

Community
  • 1
  • 1
Andrea Reina
  • 1,170
  • 8
  • 19