Passing the module as an argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • micmast
    New Member
    • Mar 2008
    • 144

    Passing the module as an argument

    Hey,

    I have question, I'm trying to pass the module itself when calling a function in another module. Let me illustrate:

    test.py
    import test2

    def hello():
    print "hello there"

    test2.call(self )



    test2.py
    def call(object):
    object.hello()


    >python test.py
    Traceback (most recent call last):
    File "test.py", line 6, in <module>
    test2.call(self )
    NameError: name 'self' is not defined

    when I google, I see that self is mostly refered to classes. Is this only for classes or did I miss a link?

    Thx in advance
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Yes, self refers to an instance of a class typically, but really you can make the keyword be anything (self is just kinda the standard). You are doing something strange in your code and I can't quite understand what it is that you're trying.

    You will want to make a class with the function hello() being of that class, then do something like the following:
    [CODE=python]
    >>> class mycls:
    ... def hello(self):
    ... print "Hello world"
    ...
    >>> my_class = mycls()
    >>> def call(obj):
    ... obj.hello()
    ...
    >>> call(my_class)
    Hello world
    >>>
    [/CODE]

    Comment

    • micmast
      New Member
      • Mar 2008
      • 144

      #3
      That works, but without using classes it isn't possible?
      I could do it like that, but then I would have to rewrite a large portion of code :)

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        You have to understand that using a '.' and function ( ie, object.hello() ) means that hello() is an attribute of object. The only way that something is an attribute is either through a module or a class.

        You could do it through another script, so that hello is an attribute as in:
        [CODE=python]
        import test
        test.hello()
        [/CODE]

        Comment

        • micmast
          New Member
          • Mar 2008
          • 144

          #5
          The thing is, I'm writing a plug in system for a program and the plug in has to be able to get some information from the programming calling the plug in in the first place. I thought I was able to fix this quickly by providing the module as a argument.

          But I guess I'll make a small class like below (probably not the fastest, but it will limit time coding)

          test.py
          Code:
          import test2
          
          def hellof():
          	print "hello from the function"
          	
          class Test:
          	def __init__(self):
          		print "loaded"
          	def hello(self):
          		hellof()
          
          obj = Test()
          test2.call(obj)
          test2.py
          Code:
          def call(object):
          	object.hello()

          Thank you very much for this fast response.

          Comment

          Working...