Thundershocked
Thundershocked

Reputation: 25

Creating an instance of a class existing in a different file(python)

I am trying to learn how to change my programs so that they use code from multiple python scripts. I have two scripts (these are big files, so cutting them down to only what's needed)

main.py

import pygame
import player #Imports the player.py script
p1 = hero("woody.png",[2,2]) #Creates an instance of hero

player.py

import pygame
import main

class hero:
    def __init__(self,image,speed):
        self.image = pygame.image.load(image)
        self.speed = speed
        self.pos = self.image.get_rect()

Running this gives me the following error:

AttributeError: 'module' object has no attribute 'hero'

I'm not quite understanding why it's trying to get an attribute instead of creating an instance. I've tried looking at other examples and how they are solving the problem but when I attempt to apply it to the code above it does not solve my issue.

Upvotes: 0

Views: 98

Answers (3)

Chris
Chris

Reputation: 22963

Like Athena said above, don't import main into player and player into main. This causes a import loop. Just simply import player into main

Secondly, you must say player.hero() if you want to use the class hero from the player module. Or if you want to just say hero(), you can say from player import*. This tells python to import all the files from player into the namespace main.

Be careful using this though, as a function or class from your player file may conflict with an already exiting function or class, with the same name.

And as a side note, classes in python general have their first letter capitalized.

here is how your files should look:

main.py

import pygame
import player #Imports the player.py script
p1 = hero("woody.png",[2,2]) #Creates an instance of hero

player.py

import pygame

class hero:
    def __init__(self,image,speed):
        self.image = pygame.image.load(image)
        self.speed = speed
        self.pos = self.image.get_rect()#.......etc

Upvotes: 0

Doron Cohen
Doron Cohen

Reputation: 1046

Drop the import main in player.py and change the last line in main.py to:

p1 = player.hero("woody.png",[2,2])

Edit: Python does not know what class/function hero is. It needs you to tell it hero is a class in the player module. And that's what player.hero means.

Also never import one module from another and vice versa. You can get an import loop which is very hard to debug.

Lastly, in python it is common to name a class with a capital letter as Hero instead of hero.

Upvotes: 0

nalzok
nalzok

Reputation: 16117

  1. To import hero from another module, you should write player.hero, or simply from player import hero.

  2. Importing player in main and main in player would cause "circular references".


Here is the modified code:

main.py

import pygame
from player import hero # Imports the player.py script

p1 = hero("woody.png",[2,2]) # Creates an instance of hero

player.py

import pygame    

class hero:
    def __init__(self,image,speed):
        self.image = pygame.image.load(image)
        self.speed = speed
        self.pos = self.image.get_rect()#.....etc

Upvotes: 1

Related Questions