PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

How do I properly reference Python classes?

I'm working on writing my first Python class. Being the java programmer that I am, I have something like this:

#class1.py
class class1:
    def __init__(self):
         #Do stuff here

And in my current script:

import class1
object = class1()

I'm getting a Name Error: name 'class1' is not defined

I've also tried this, with no luck:

import class1
object = class1.class1()

The error I get here is AttributeError: 'module' object has no attribute 'class1'

What am I doing wrong?

Upvotes: 1

Views: 110

Answers (2)

Max Fellows
Max Fellows

Reputation: 392

Python import is by module and then by the contents of the module, so for your class1.py it becomes:

from class1 import class1

Python module docs

Upvotes: 4

Sudip Kafle
Sudip Kafle

Reputation: 4391

In Python you import the modules. For class1.py file you can use:

from class1 import class1

Or if you have more than one....

from class1 import *

Upvotes: -1

Related Questions