Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    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 **


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •