Title: RE: Class defined in other file not working
Yea that works.  WHy the hell didnt it say that in any of the on line tutorials!???
 
Thanx Everyone.
 
 
-----Original Message-----
From: Horkoff, Dave [mailto:[EMAIL PROTECTED]]
Sent: Thursday, May 30, 2002 10:17 AM
To: 'Jose Guevarra'; Activepython List
Subject: RE: Class defined in other file not working

Use the following line to instantiate a instance of Cloud in your stand alone program...

#import cloud w/ the new Cloud class
cloud = SDsimLib.Cloud('cloud',[1,2,10])
        ~~~~~~~~~


If you wanted to access Cloud directly in your namespace, without having to use the ModuleName-Dot-Class format, change your import as follows...

from SDsimLib import Cloud


Keep it up!
David.


-----Original Message-----
From: Jose Guevarra [mailto:[EMAIL PROTECTED]]
Sent: May 30, 2002 1:29 AM
To: Activepython List
Subject: Class defined in other file not working


Hi,

 I created a class and placed it into another file

then imported that file into my main script but it comes up saying that the
variable 'myclassname' doesn't exist.

Both files are in the same directory and the class  works fine when used in
the same script.

Here is what I have. Can anyone look over them and see what im doing wrong?
Im still new to classes/python

1- the stand alone file

2- module w/ class definition

3- main script that imports module and calls on class 'Cloud'



thanx,

##############ERROR- when trying to use imported module
SDsimLib.py#######################
VRUT  2.5c
Traceback (most recent call last):
  File "D:\Program Files\Komodo-1.2\callkomodo\kdb.py", line 397, in
_do_start
    self.kdb.run(code_ob, locals, locals)
  File "d:\python20\lib\bdb.py", line 346, in run
    exec cmd in globals, locals
  File "C:\Documents and Settings\jose\Desktop\Fproject\SDsimv0.3.py", line
123, in ?
    cloud = Cloud('cloud', [1,1,1])
NameError: There is no variable named 'Cloud'
#############################################

#this is where I define the class in its own file and create an instance and
check if it works, which it does.

########--------------------------------------------------------------------
--

import vrut  # import the 3d evironment module


vrut.go()  #initialize the vrut universe

class Cloud:  # Class for importing a cloud and initializing it's attributes

    def __init__(self,name,POS): #name is a string, POS is list[x,y,z]

        self.pos = POS
        self.name = name
        self.obj = vrut.addchild('models/cloud.wrl') #import the vrml 3d
object
        self.x = POS[0]
        self.y = POS[1]
        self.z = POS[2]
        self.obj.translate(self.x,self.y,self.z) # move it to the user given
initial coordinates

    def Move(self,POSx,POSy, POSz): #this method moves the object and
updates it's 3d coordinates

        self.x = POSx
        self.y = POSy
        self.z = POSz
        self.obj.translate(self.x,self.y,self.z)


cloud = Cloud('cloud',[1,2,12]) #create an instance which shows up at the
coordinates given
print cloud.z #print out its current z coordinate

cloud.Move(0,0,20) # move it to a new spot

print cloud.x # print out its new x coordinate

--------------------------------------------------------------------
#This is the module where the class is defined
#These are the definitiions of our VR Skydiving simulation

#Import our modules
import vrut
import math
import time
import string


VERBOSE = 0


#You can access these like this
# mysphere = ImportSphere(Initx,Inity,Initz)
# You then have accss to the .x, .y, .z
#def ImportSphere(POSx,POSy,POSz):
#
#    Ball = vrut.addchild('tut_sphere.wrl')
#    Ball.x = POSx
#    Ball.y = POSy
#    Ball.z = POSz
#    Ball.translate(Ball.x,Ball.y,Ball.z)
#    return Ball

class Cloud:

    def __init__(self,name,POS):

        self.pos = POS
        self.name = name
        self.obj = vrut.addchild('models/cloud.wrl')
        self.x = POS[0]
        self.y = POS[1]
        self.z = POS[2]
        self.obj.translate(self.x,self.y,self.z)

    def Move(self,POSx,POSy, POSz):

        self.x = POSx
        self.y = POSy
        self.z = POSz
        self.obj.translate(self.x,self.y,self.z)

#--------------------------------------------------------

NOW THIS IS WHERE I IMPORT AND CALL THE CLASS FROM THE MAIN SCRIPT


#This is a practice script for testing the sky diving simulation

#Import any needed modules
import vrut
import math
import time
import string
import SDsimLib  #name of the file w/ class is defined


##lots of code here

#import cloud w/ the new Cloud class
cloud = Cloud('cloud',[1,2,10])



_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython

Reply via email to