473,837 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a function from module question.

Is there any way I could have the following work?

First I would have a module define a function to do
something like print some data.

----- module_name.py -----

[snip]

def print_this(data ):
print "This is the data: %s" %data

[/snip]

-----------------------------
Then I would have a script that uses the
print_this function defined in the module
without using the module name in the call.

----- test_file.py -----

[snip]

import module_name.py
print_this("lot s of data")

[/snip]

----------------------

Now, I know I can call the function using
module_name.pri nt_this("lots of data")
but can using the module name at the beginning
be avoided?

If not, why? I am sure there is a good pythonic
explanation.

Thanks
Jul 18 '05 #1
9 1637
Sean wrote:
Then I would have a script that uses the
print_this function defined in the module
without using the module name in the call.


from module_name import print_this

or, even:

from module_name import print_this as other_nice_name

--Irmen
Jul 18 '05 #2
> Sean wrote:
Then I would have a script that uses the
print_this function defined in the module
without using the module name in the call.


from module_name import print_this

or, even:

from module_name import print_this as other_nice_name


So what if I have a whole bunch of functions - say 25 of them.
Is there a way to do this without naming each function?
Jul 18 '05 #3
Sean wrote:
Sean wrote:
Then I would have a script that uses the
print_this function defined in the module
without using the module name in the call.


from module_name import print_this

or, even:

from module_name import print_this as other_nice_name


So what if I have a whole bunch of functions - say 25 of them.
Is there a way to do this without naming each function?


Yes [1], but it's basically deprecated and you shouldn't use it.
Consider refactoring your code.

Steve

[1] http://docs.python.org/ref/import.html
Jul 18 '05 #4
>>>from module_name import print_this

or, even:

from module_name import print_this as other_nice_name


So what if I have a whole bunch of functions - say 25 of them.
Is there a way to do this without naming each function?


Yes [1], but it's basically deprecated and you shouldn't use it. Consider
refactoring your code.


Refactoring my code? Sorry, I am not sure what you mean here.

How would one refactor the example in my original post?
Jul 18 '05 #5
Sean wrote:
from module_name import print_this

or, even:
from module_name import print_this as other_nice_name

So what if I have a whole bunch of functions - say 25 of them.
Is there a way to do this without naming each function?


Yes [1], but it's basically deprecated and you shouldn't use it. Consider
refactoring your code.


Refactoring my code? Sorry, I am not sure what you mean here.

How would one refactor the example in my original post?


The original post only had one name to import, not 25, so refactoring
isn't really necessary. ;) What are the 25 functions you want to
import? Perhaps you can group them together in classes? Or maybe a
couple of (sub-)modules is the way to go...

STeVe
Jul 18 '05 #6

"Sean" <se**@buildingo nline.com> wrote in message
news:UTsQd.3266 9$6u.27954@fed1 read02...
import module_name.py


leave off the .py

Irmen answered your main question.

Terry J. Reedy

Jul 18 '05 #7
Sean wrote:
So what if I have a whole bunch of functions - say 25 of them.
Is there a way to do this without naming each function?


Yes [1], but it's basically deprecated and you shouldn't use it. Consider
refactoring your code.


Refactoring my code? Sorry, I am not sure what you mean here.


'Refactoring' is just a fancy way of saying 'reorganizing'. What it
means in this case is to look at the reason that you have 25 functions
in this other module whose name you don't want to type. Perhaps
reassembling those functions into a class or two will let you have
fewer names to import, or perhaps there's no compelling reason for
them to be in a different module to begin with. (Or, more likely, you
should just not worry about using the module name. It's really better
to keep track of where all of your names come from, and fully
qualified names do that nicely. What do you see as the harm of using it?)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #8
Sean wrote:
Sean wrote:

Then I would have a script that uses the
print_this function defined in the module
without using the module name in the call.


from module_name import print_this

or, even:

from module_name import print_this as other_nice_name

So what if I have a whole bunch of functions - say 25 of them.
Is there a way to do this without naming each function?


You do that like so: "from module import *". But you should avoid that,
as stated in the Python help:

Note that in general the practice of importing * from a module or
package is frowned upon, since it often causes poorly readable code.
However, it is okay to use it to save typing in interactive sessions,
and certain modules are designed to export only names that follow
certain patterns.

The "certain patterns" usually occur in huge packages, such as in the
various GUI toolkits. E.g., all of the exported PyQt classes are
prefaced with Q (QButtonGroup, QTabWidget), so doing "from qt import *"
is fairly safe.

You can also import a module like so: "import module as m" to save on
some typing, if that is your concern. But namespaces are a feature of
Python, not a limitation, so the Python way is to use them for clearer
code. With a large number of functions like that, it sounds more like
you should be inheriting from a class anyway, which I think is what
Steven Bethard meant when he suggested refactoring.

For more information on the Python way, go to the Python interpreter and
type "import this" ;>)

--
Soraia: http://www.soraia.com

Jul 18 '05 #9
Sean, if you are asking what I think you are asking (I don't think name
hiding is the issue), you can use

from module_name import *

and you will end up with all of the functions at session scope. You can
use the 'as' to alias the function names if you wish

from module_name import fn1 as myfn1, fn2 as myfn2

but, um, that gets confusing.

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2167
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string containting a template for a tuple, the function then finds a tuple that matches this template and then returns it. The template may contain variable names, which match against anything in that position in the tuple - this is all working fine,...
2
4461
by: Marc Shapiro | last post by:
I am relatively new to python (I have used it on and off for a few small projects over the last few years) so I imagine that what I am trying to do has already been done, but practical experience, even if it is reinventing the wheel, is still useful, so... I am trying to write a module to handle drop down menus using curses (on linux). Curses seems to have a wrapper for the panels library, but not forms, or menus. I am not even sure...
1
1584
by: Stephen Kellett | last post by:
Hello everyone, I'm trying to do something in C calling Python and its failing. I'd be grateful if you could take a look and hopefully you have an answer. What I'm trying to do is determine the address of the "collect" function in the "gc" module. I want to do this so that we can determine when a garbage collection happens, regardless of how it is triggered (explicitly by a user call, or implicitly by the behaviour of the program...
6
3316
by: jchao123 | last post by:
Dear All, I have an MDB file (Access 2000/XP) which contains generic routines I use in various apps (eg, API calls, File access classes etc). I have compiled into an MDE file which I reference in VBA from my other Access applications. This works fine and I'm quite happy with it... except for one area; error handling. In most of my functions I call an error handler which
1
3175
by: Nick | last post by:
I have read many postings regarding the above thread but don't seem to have found the exact problem I am experiencing. I am using VS.NET and have 2 projects within the 1 solution. First project is a VB module (compiled to .EXE) and is the calling module. The 2nd project is C and compiles to a DLL. The DLL takes the definition of an ARC(4 Points) and converts this geographically into a string of Points. The DLL was written in C because...
19
4275
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
2
1434
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! **************************************************************************************************** Original Question: -------------------- Has anyone called a COBOL subroutine using COBOL CALL from a COBOL/DB2
5
7857
by: Stinky Pete | last post by:
Hi (again) ;-) I'm still very much at the bottom of a steep learning curve with VB, so any and all help is always appreciated. I've found some code to generate the user names who have logged onto the file in question and from what I've been reading it will do what I need. However, what is eluding me is the "calling" it bit. If anyone can put into simple terms what is needed & general principles I would be really grateful.
4
1523
by: MLH | last post by:
I have the following saved UNION query named qryPeople2NameInNPaperAd: SELECT & " " & & " " & & " " & & ", " & & " " & AS Item, tblVehicleJobs.VehicleJobID FROM tblVehicleJobs INNER JOIN tblAddnlOwnrs ON tblVehicleJobs.VehicleJobID = tblAddnlOwnrs.VehicleJobID Where tblVehicleJobs.VehicleJobID = GetCurrentVehicleJobID(); UNION SELECT & " " & & " " &
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9683
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10633
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7814
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5674
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.