Python 2.6: Running other .py?
Hey,
I was wondering if there is a function, or something, that would allow me to run another .py file from the script I am running? Sort of like "import", but being able to do it more then once.
Sort of like classes in Java or C, that allow you to break up the program into parts and have all the parts reference each other?
I have found semi-functional ways to do this using import by:
Code:
import stuff
...(Runs the stuff file)
del stuff
continues program,
but being able to do "import stuff" again
Anyhow, I'm just curious for an more effective method.
Re: Python 2.6: Running other .py?
You can write classes in python. For example,
Code:
#!/usr/bin/python
class Test:
one = 1
def add_num(self,num):
return self.one + num
print Test.one
tmp = Test()
tmp.one = 2
print tmp.add_num(3)
Self in python is almost like this in java. As you can see I create a new Test object set its one property to two (lol). Then call the add_num function. Run this to see the results.
Re: Python 2.6: Running other .py?
I see what your saying. Its not quiet what I was thinking, but it did give me a better idea.
I had the idea to create a module which would define the action of the whole code as a function.
Module Greet
Code:
def intro(name,age):
print "Hello",name,"who is",age,"years old!"
print "My name is Bob!"
The script:
Code:
import Greet
myname = raw_input("Whats your name?")
myage = raw_input("Whats your age?")
Greet.intro(myname,myage)
What do you think about doing something like that? :p
Re: Python 2.6: Running other .py?
Good! You made a excellent conclusion from my example, that way you can define extra classes and methods and put them in other modules and import them just like you did. Much cleaner code that way don't you think?
Re: Python 2.6: Running other .py?
Indeed, is will allow me and my friend to work on each part of the game we are building with out needing to constantly be integrating and merging code!
Now I can build with the Crafting module, while he works on the Fighting module!
Re: Python 2.6: Running other .py?
If you're working on a collaborative project, I would strongly recommend you look into some sort of versioning software. It will make your life immensely easier.
I personally like Git.
Re: Python 2.6: Running other .py?