1

I come from a mostly Java background, but have recently been delving into some Python. I've been mostly getting it, but there's some syntax that seems really weird to me. I have this project I'm working on that contains multiple files/ classes. I have one class, Mesh.py:

class Mesh:
    def __init__(self, name):
        #dostuff

that I want to instantiate in another file, Main.py. I noticed two things,

  1. I have to import Mesh, which seems odd to me, since it's in my project,
  2. To create a Mesh I have to do this:

    mesh = Mesh.Mesh('name')
    

which seems super awkward. Why can't I just do

mesh = Mesh('name')

Am I doing something wrong here, or is this just an unavoidable part of Python?

2
  • 2
    Java gets to assume that class Foo is in file Foo.java. Python doesn't have that requirement, so you need to give both the file and class name. As you learn more languages, you should get more comfortable with different conventions like tis. Roll with it. Commented Jan 17, 2015 at 3:41
  • Having a different name for your module and for your class might clarify what's going on here. Commented Jan 17, 2015 at 3:47

5 Answers 5

5

In Python, every file you create is a module and a collection of modules can be logically grouped as a package. Python is little different than Java, which allows files in the same package to be used without importing explicitly.

Since a module is a namespace, whenever you use an identifier in a module, it must be defined in it, otherwise you will get a NameError.

When you import a module,

import Mesh

Python understands that you have imported a module and all the items in it can be referenced with the module's name. If you do

Mesh('name')

you will get an error, because only the Mesh module is imported in the current module, not the Mesh class. That is why you have to explicitly specify

Mesh.Mesh('name')

It means that, you are telling Python that, I am referring to the Mesh class in the Mesh module, which I imported.

But in your case, you can import only the particular class from the Mesh module into the current module's namespace, like this

from Mesh import Mesh

And then create an object of it, like this

Mesh('name')
Sign up to request clarification or add additional context in comments.

Comments

2

Well you can name your Mesh.py something else more englobing, unlike Java you don't need class names in your .py module, and doesn't have to contain only one "top level class"

Otherwise you can also do

from Mesh import Mesh
Mesh('name')

Comments

1

I have one class, Mesh.py

Right there is the problem. Mesh.py is not a class, it's a file, which means it's a module.

In Python, you shouldn't automatically follow the Java pattern of putting each class in its own file. Break things up between multiple files if and when they become too large and unwieldy to fit in one file. If you have many small classes (and/or functions and other things), just put them in one file. Then you won't have to import anything. If you get to the point where you need to split things up among multiple files, you won't see imports as a problem, because at that point your codebase will be complex enough that the separation into multiple modules will increase code clarity.

Comments

1

If you working with modules, you should know, for example random module has a method randint. If you just do;

import random

You have to use randint method like;

random.randint()

But if you want to use only randint method, you can do;

from random import randint

Then you can use this like;

randint()

So your module name is Mesh.py, if you import it like;

import Mesh

You have to use your Mesh method in Mesh module like;

Mesh.Mesh()

As you see above on random module. So you have to import it like;

from Mesh import Mesh
Mesh()

Because your module name is Mesh, if you save your module as ChuckNorris then you have to do this;

from ChuckNorris import Mesh
Mesh()

or;

import ChuckNorris
ChuckNorris.Mesh()

Remember in Python, every script is basically a module. You are confused because your script/module name is Mesh and this script/module has a method called Mesh. That's why you if you do import Mesh you have to use it Mesh.Mesh(). Try to save your module name different, you'll understand.

Comments

0

You don't have to import at all if you are referencing the class inside the module (i.e. Mesh.py) that has the definition. In that case, Mesh (the class) is in the module level namespace. That's worth noting because you can have as many classes a you want in a module.

The other way to get Mesh (the class) into the module level namespace is to import it from the module in which it is defined (or any other module that has imported it). Like:

from Mesh import Mesh
mesh = Mesh('name')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.