Tuesday, 15 July 2014

python - Importing from a module and avoiding having to make duplicate imports/variables -



python - Importing from a module and avoiding having to make duplicate imports/variables -

i 1 time 1 time again find myself here, desperately begging answers :)

so have problem regarding imports in python. have main code importing secondary module contains single class. trying import , utilize class without having import within of secondary module. however, python catching me , saying things don't exist etc etc so.

file "c:\users\cr0ss0v3r\desktop\pathogen\mutations.py", line 24, in draw pygame.circle.draw(screen, self.color, (self.posx, self.posy), 5, 0) nameerror: global name 'pygame' not defined

python asking me import pygame again. want class code loaded secondary module can utilize in main program. can not create instance of screen must utilize 1 graphics beingness drawn screen. love avoid redundancy of importing modules again. want programs clean, oop wise. creating separate file classes much ask? please help. i've been night looking answers no avail.

prepare wall of text.

this secondary module's entire code. (a single class.)

class radiantregrowth: def __init__(self): self.name = "radiant regrowth" self.posx = 0 self.posy = 0 self.spawned = false self.color = (0,255,0) def applyaffect(self, target): if self.spawned == true: target.health += 1 else: pass def spawn(self, x, y): spawnx = random.randint(50, x-50) spawny = random.randint(50, y+50) self.posx = spawnx self.posy = spawny self.spawned = true def draw(self): pygame.draw.circle(screen, self.color, (self.posx, self.posy), 5, 0)

it's catching me @ lastly line, regarding utilize of pygame defined within of definition within of class. , yes code cluttered , total of useless trivial bits, @ point want limps.

i importing so.

from mutations import *

and using simple phone call (the programme isn't getting far, fyi.) after few negligible sections of code.

radiantregrowth = radiantregrowth()

this first phone call secondary module. , said; programme halts tries import secondary module mutations due fact wants me import pygame again, , set screen, again. not going create global.

sorry redundancy. trying maintain clear possible.

if code within class wants phone call functions pygame, need import name within module lives in. way python works.

you don't want set new screen instance. instead, should pass class - either straight draw method, or class when initialize store instance attribute , refer within draw.

(and believe pygame.draw.circle, not pygame.circle.draw.)

python import module pygame redundancy

No comments:

Post a Comment