473,508 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return variables from modules ??

Hi,

I am new to Python and am currentky just playing with some simple functions however
I can not work out how to return a variable back from a module, probably easier if you see the code..
what I want it to do is to repeat the while loop until it is no longer equal to 'repeat',
code is below, go easy on me!:
================================================== =========================
import sys
import os
# Check that the folder is accessible and writeable
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK | os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!
sys.exit()

================================================== ============================

I have tried setting reply as a global variable but to no avail, I assume that I need to pass the variable
back from the chkpth module but I do not know how to, any help appreciated.

Regards

Rig
Jul 18 '05 #1
6 1739
Rigga wrote:
Hi,

I am new to Python and am currentky just playing with some simple
functions however I can not work out how to return a variable back
from a module, probably easier if you see the code.. what I want
it to do is to repeat the while loop until it is no longer equal
to 'repeat', code is below, go easy on me!:
================================================== ========================= import sys import os
# Check that the folder is accessible and writeable
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!
sys.exit()

================================================== ============================
I have tried setting reply as a global variable but to no avail, I
assume that I need to pass the variable back from the chkpth
module but I do not know how to, any help appreciated.


A common way to do this is to put the code in your module into a
function (in that module) and at the end of that function, return
a value. Then do:

import mymodule
reply = mymodule.myfunction()

And, here is how you might modify your module:

================================================== =========================
import sys
import os

# Check that the folder is accessible and writeable
def myfunction():
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth
result
print reply # always prints repeat no matter what!
return reply
================================================== ============================

Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dk******@rexx.com
Jul 18 '05 #2

Rigga> I am new to Python and am currentky just playing with some simple
Rigga> functions however I can not work out how to return a variable
Rigga> back from a module...

This is not possible. Modules are not callable objects. They define
namespaces which hold other objects, among which are functions, which are
callable.

Your module becomes something like

import sys import os

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK | os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

if __name__ == "__main__":
# Check that the folder is accessible and writeable
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!

Suppose the above code is in chkpth.py. You can execute

python chkpth.py

to run it standalone, or from other Python code write:

import chkpth

...

result = chkpth.chkpth(somepath)

Skip

Jul 18 '05 #3
I have tried the code you supplied however it still does not return the
reply as expected, I have even tried the following code but I just cant get
it to return a variable from a module:

My module: we will call it filechk.py
************************************************** ******************
import sys
import os

def myfunction():
reply = 'repeat'
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):
if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!
return reply

************************************************** ******************

Main program: we will call it testme.py

************************************************** ******************
import filechk
result = filechk.myfunction()
print result

************************************************** ******************
results when ran:
---------------------------------------------------
root@mybox:> python testme.py
root@mybox:> Enter Path to files: /root/
root@mybox:> None
What I expected to happen in the example above for it to return OK (and if
I queried the reply variable for it to return 'stop)

What am I doing wrong?
Jul 18 '05 #4
On Wed, 22 Oct 2003 17:09:54 +0100, Rigga wrote:
I have tried the code you supplied however it still does not return the
reply as expected, I have even tried the following code but I just cant get
it to return a variable from a module:

My module: we will call it filechk.py
************************************************** ******************
import sys
import os

def myfunction():
reply = 'repeat'
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):


Here you define chkpth, but you never call it... Why have you made this
into a separate function inside of myfunction? It seems you should remove
this def and unindent everything under it.

-Mark

Jul 18 '05 #5
Rigga <Ri***@noemail.com> wrote in news:pZxlb.10028$xv5.9058@news-
binary.blueyonder.co.uk:
What am I doing wrong?


You are assigning to the local variable 'reply' inside the function chkpth.
This is not the same as the variable 'reply' in 'myfunction'.

You seem to be keen on the idea of reusing variable names, as I see you
also have a local variable 'chkpth' inside the function 'chkpth'. Python is
a free programming language, you don't actually get charged for each new
variable you use, so be generous and use some different names.

You also seem to have a slight indentation problem so it looks like chkpth
(the function, not the variable) is never going to get called.

Try something like this:

import sys
import os

def myfunction():
# assignment to outer reply variable removed from here.
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):
if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

# Returning the reply and the chkpth variable
return reply, chkpth
# Pick up the results so we can use them
reply, result = chkpth(FilePath) # used to show me chkpth
result
print result
print reply # always prints repeat no matter what!
return reply

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #6
Duncan Booth wrote:
Rigga <Ri***@noemail.com> wrote in news:pZxlb.10028$xv5.9058@news-
binary.blueyonder.co.uk:
What am I doing wrong?


You are assigning to the local variable 'reply' inside the function
chkpth. This is not the same as the variable 'reply' in 'myfunction'.

You seem to be keen on the idea of reusing variable names, as I see you
also have a local variable 'chkpth' inside the function 'chkpth'. Python
is a free programming language, you don't actually get charged for each
new variable you use, so be generous and use some different names.

You also seem to have a slight indentation problem so it looks like chkpth
(the function, not the variable) is never going to get called.

Try something like this:

import sys
import os

def myfunction():
# assignment to outer reply variable removed from here.
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):
if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

# Returning the reply and the chkpth variable
return reply, chkpth
# Pick up the results so we can use them
reply, result = chkpth(FilePath) # used to show me chkpth
result
print result
print reply # always prints repeat no matter what!
return reply

er yeah well spotted!! I did get a bit confused didnt I lol. THanks for
clearing it up for me it all maks sense now.

Cheers

RiGGa
Jul 18 '05 #7

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

Similar topics

7
16725
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
6
1576
by: dkeeney | last post by:
I have a newby type question on how global variables work between modules. I have a module test2.py that defines a global variable as well as two functions to operate on that variable. A script...
4
1604
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
6
677
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
5
3306
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
7
3107
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
4
7217
by: gamja | last post by:
Hi all. I know that some padding bits are inserted between data members of a structure. Is this rule also applied for the variables on local stack or global??? For example, in following code...
25
66039
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
8
2691
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
0
7385
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...
1
7046
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...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
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...
0
3195
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...
0
1558
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 ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.