473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Passing the module as an argument

micmast
144 100+
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
Mar 13 '08 #1
4 1446
jlm699
314 100+
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:
Expand|Select|Wrap|Line Numbers
  1. >>> class mycls:
  2. ...     def hello(self):
  3. ...         print "Hello world"
  4. ...     
  5. >>> my_class = mycls()
  6. >>> def call(obj):
  7. ...     obj.hello()
  8. ...     
  9. >>> call(my_class)
  10. Hello world
  11. >>> 
  12.  
Mar 13 '08 #2
micmast
144 100+
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 :)
Mar 13 '08 #3
jlm699
314 100+
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:
Expand|Select|Wrap|Line Numbers
  1. import test
  2. test.hello()
  3.  
Mar 13 '08 #4
micmast
144 100+
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
Expand|Select|Wrap|Line Numbers
  1. import test2
  2.  
  3. def hellof():
  4.     print "hello from the function"
  5.  
  6. class Test:
  7.     def __init__(self):
  8.         print "loaded"
  9.     def hello(self):
  10.         hellof()
  11.  
  12. obj = Test()
  13. test2.call(obj)
  14.  
test2.py
Expand|Select|Wrap|Line Numbers
  1. def call(object):
  2.     object.hello()
  3.  

Thank you very much for this fast response.
Mar 13 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
3
by: Scott | last post by:
What is the proper syntax for sending an argument from a form contol to a subroutine in a module? For instance, from a textbox on a form I call a module subroutine from the textbox's OnUpdate...
4
by: Dave Cullen | last post by:
I want to call a function using my own data type as an argument. The declaration for my function looks like this: Public Function CreateCardInfo(ByVal data As WoData) VB.NET balks at this,...
0
by: VaBa | last post by:
Hi, Need some help.. I have a VBA class module in MS ACCESS. In the same MS ACCESS app, I am calling a .NET DLL (which i have exposed as COM-visible) and passing BYREF an instance of the VBA class...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.