[wingide-users] Question on calling SubroutineA in SubroutineB

Jeff Vahue jvahue at knowlogicsoftware.com
Thu Nov 14 08:24:30 MST 2013



In addition to your current reading I highly recommend you read Dive into Python.  It’s free online and very well written.  By working through some of the examples you will see how to develop truly complex Python programs.  You can probably work through most of Dive into Python in a few hours and you will be amazed at how simple the answer to your question is.


To answer your question you need an import statement at the top of Test1.py.  


This is a fairly basic concept to Python and many other languages and a quick read of Dive into Python will answer this an many more of your questions, as I’m sure you have more as you get deeper into Python.


Jeff Vahue

90 Annawon Ave.

Wrentham, MA. 02093


www.knowlogicsoftware.com <http://www.knowlogicsoftware.com/> 

Knowlogic Software25717FinalLogoD6L1(RGB)

NOTICE OF CONFIDENTIALITY - THIS MESSAGE IS INTENDED ONLY FOR THE ADDRESSEES. It may contain privileged, confidential or proprietary information. Any unauthorized disclosure, use, copying or distribution of the contents of this message is strictly prohibited. If received in error, please promptly notify the sender and delete the original and all copies from your system.   



From: wingide-users-bounces at wingware.com [mailto:wingide-users-bounces at wingware.com] On Behalf Of Joseph POLLACCO
Sent: Thursday, November 14, 2013 4:13 AM
To: Yarko Tymciurak; Wingware Support
Cc: wingide-users at wingware.com
Subject: Re: [wingide-users] Question on calling SubroutineA in SubroutineB


Dear John and Yarko Tymciurak, 


Many thanks for responding to my question so promptly. 

Thanks for introducing me to MumPy/ SciPy which will make my transition from MATLAB to PYTHON easier. To date I am using an excellent textbook Think Python (How to think like a computer scientist).

Many thanks for informing me that PYTHON reads from top to bottom.

def test2():

     return 'hello' 


def test1():

     return test2() 


print( test2() )


Now I have an other question, which worked well when I started using an interpreter like bpython but to date not with WINGWARE:


Saved as TEST2.py

def TEST2():

     return 'hello' 



Saved as TEST1.py

def TEST1():

     return TEST2() 


print( TEST2() )


Running TEST1



File "C:\JOSEPH\PYTHON2\TESTING\TEST1.py", line 4, in <module>
       print( TEST2() )

        NameError: name 'TEST2' is not defined


I have performed the following action which did not help: 


1) Created a NewProject;

2) Add existing directory

3) Project properties -> Environment -> Python path (Custom)


Many thanks for any help you may provide to help me call functions defined by the users.

Kind regards, 

Joseph A. P. POLLACCO 



Joseph Alexander Paul POLLACCO


(: (+33) 1 69 86 16 61

)skype: joseph_pollacco

www.pollaccowater.org <http://www.pollaccowater.org/hydrology/Research.html> 




------ Original Message ------

From: "Yarko Tymciurak" <yarkot at uchicago.edu>

To: "Wingware Support" <support at wingware.com>

Cc: "Joseph POLLACCO" <pollacco.water at gmail.com>; "wingide-users at wingware.com" <wingide-users at wingware.com>

Sent: 13/11/2013 18:19:01

Subject: Re: [wingide-users] Question on calling SubroutineA in SubroutineB


On Nov 13, 2013, at 9:52 AM, Wing IDE Support <support at wingware.com> wrote: 


 On 11/13/13 3:46 AM, Joseph POLLACCO wrote: 

 I am new to the WingIDE 5community and I have a trivial question 

 concerning how to call another subroutine. This action is easily 

 performed in MATLAB and in FORTRAN but I do not know why this does not 

 work for me in WingIDE. 


 For example, calling the subroutine *TEST1(a,b) *[works well 

 independently] from subroutine TEST2. 


 Where are you defining your subroutines / functions? In Python, it's typical to define multiple functions in one file, as in: 


You might want to try this in a file outside of wing, perhaps bpython. 

What you have going on is that python is an interpreter, which reads the file in order. 

What is likely going on is that your first function is “read” and executed before the second function’s definition is read, 

i.e. before the second function even exists in the interpreter’s dictionary of names. 


The typical way to address this in python is to define a function near the bottom of a file (a “main()”) which then delegates 

to functions. In this way, all the function definitions have been read before an attempt to execute them. 


For example, 


def test1(): 

     result = test2() 


def test2(): 

     return “hello” 


# this will work - but if this line is at top of file, it won’t; 


my_total_result = test1() 



You can also import from the current directory into the top of a file, if readability is your concern, and 

you’d like to just have the logic that “gets on with it” at the top of the file. 


Google for “python forward reference” and “import” and that should get you started. 

bpython (bpython-interpreter.org) is a nice interactive tool, with nice completions to play around with (and save your results). 




 def test1(): 



 def test2(): 



 Note that this is more of a question about Python than about Wing IDE. You may want to look at some of the introductions to Python for programmers found on https://wiki.python.org/moin/BeginnersGuide/Programmers and elsewhere. I also found http://wiki.scipy.org/NumPy_for_Matlab_Users through google, which could be helpful if you use NumPy / SciPy. 







 Wing IDE users list 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/wingide-users/attachments/20131114/8ff6b6fa/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 4149 bytes
Desc: not available
URL: </pipermail/wingide-users/attachments/20131114/8ff6b6fa/attachment-0001.jpg>

More information about the wingide-users mailing list