Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-18-2011, 03:53 AM   PM User | #1
mikemrm
New Coder

 
Join Date: Mar 2009
Location: Texas
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
mikemrm is an unknown quantity at this point
Thumbs down executing a def from a py file being executed by that file...

not sure how i would search something like that up...

Theres this game that im looking at that ive modded a bit, and so theres a core file that import the addon file in that addon file there is the __init__(self, core) making it possible to execute defs inside the core so they do something like this
<core>
class core:
def printHello():
print "Hello Moto"

def importme():
importcode

</core>
<addon>
class Addon:
def __init__(self, core):
self.core = core

def saysomething():
self.core.printHello()
</addon>

and if you executed saysomething it would run printHello

im not sure how they were able to pass the class over to that class as if you were importing it...


i hope this is clear enough, please help.
__________________
The Game
mikemrm is offline   Reply With Quote
Old 07-08-2011, 05:04 PM   PM User | #2
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
It would make sense if core.py looked like:
Code:
class core:

    def printHello(self):
        print "Hello Moto"
and the script with AddOn (addon.py) looked like:
Code:
class AddOn:

    def __init__(self, core):
        self.core = core

    def saySomething(self):
        self.core.printHello()
All you have to do is:
Code:
from core import core
from addon import AddOn

# Instantiate AddOn, passing to it an instance of core.
addon_instance = AddOn(core())

# Call saySomething.
addon_instance.saySomething()
That should say "Hello Moto".
__________________
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **
Samhain13 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:00 AM.


Advertisement
Log in to turn off these ads.