[Tutor] (no subject)

Liam Clarke cyresse at gmail.com
Wed Dec 1 01:56:09 CET 2004


Not sure quite what you mean, 

but I guess you have 

def doThisFunc(firstVar, secondVar):
        #do something 
        return aVar, anotherVar

def aFunc():
  (x, y)=doThisFunc(10,20)


so aFunc calls doThisFunc and passes the variables, and assigns the
returned results.

You may also want to know this -

(from Python Docs)

Function call semantics are described in more detail in section 5.3.4.
A function call always assigns values to all parameters mentioned in
the parameter list, either from position arguments, from keyword
arguments, or from default values. If the form ``*identifier'' is
present, it is initialized to a tuple receiving any excess positional
parameters, defaulting to the empty tuple. If the form
``**identifier'' is present, it is initialized to a new dictionary
receiving any excess keyword arguments, defaulting to a new empty
dictionary.

**identifiers always go last.

So, for instance 

    def someMaths(*tupleONum):
    sum = 0
    for item in tupleONum:
        sum + = item

    return sum

print someMaths(1,2,3,4,5) 

15

whereas normally - 

def sum2Numbers(numA, numB):
    sum = numA + numB
     return  sum

print sum2Numbers(1,2,3,4,5) will give an error.


OK, I have a function - 

def hadOne():
     x = x / 2
      return x

def doThis():
     x=10
      hadOne()

will generate a 'unbound local variable' error for function hadOne(),
as it doesn't grab the x automatically.

You either need to - 

def doThis():
     x = 10
     global x
      hadOne()

Which is bad, as globals mess up namespace badly. 

or 

def hadOne(x):
      x= x/2
      print x
      return x

def doThis():
     x=10
     x2=hadOne(x)
     print x, x2

will give - 

5
10, 5

As you can see the 'x' within hadOne() is a different 'x' to the one
within doThis(). You need to explicitly assign, or use globals. Try
not to use globals.

And within a class - 

class MyApp(bg):
      
   self.Bla=10
   
    def aFunc():
        print Bla - 5

    def bFunc():
        Bla = 7
        print Bla - 7

x=MyApp()

x.aFunc() will produce an error of 'variable Bla not found', whereas
x.bFunc() will work fine.

If you were to either do this - 

def aFunc():
      print self.Bla -5

x.aFunc() would print 

5

Or you could set up aFunc like so - 

def aFunc(Bla):
      print Bla - 5

x.aFunc(10)

5

print x.Bla

10

x.aFunc(x.Bla)

5

Confused yet? :D

Basically each variable only exists within it's function, unless it's
specifically a global or a  self.var value in a class. So, the values
have to be passed to functions and returned explicitly.

And globals are bad. : ) Try using them repeatedly and see what Python
has to say about that.

And, I think you'd use the threading.Timer within a thread controlling
module, so I'd assume that you wouldn't want to be receiving data back
from the called function. (Experts? I'm guessing like crazy here.)

Standard disclaimer - I know very little on this, a more concise and
elegant method or example is likely to be posted, and you should
always trust the advice of Danny Yoo, Kent Johnson, Alan Gauld, Bill
Mill, and I'm sure I'm forgetting ten others.

Oh, and check out Alan's excellent tutorial at
http://www.freenetpages.co.uk/hp/alan.gauld/

I hope this helps, 

Liam Clarke


On Tue, 30 Nov 2004 15:10:10 -0800 (PST), Jeff Peery
<jeffpeery at yahoo.com> wrote:
> 
> that will work! thanks. 
>   
> just for reference, how do variables pass between functions and nested
> functions?
> 
> 
> 
> Liam Clarke <cyresse at gmail.com> wrote: 
> 
> 
> Hi, 
> 
> Do you need to use the threading module? Or are you using solely the timer?
> 
> You could do a -
> 
> import time
> 
> while 1:
> time.sleep(30)
> dataBack=update()
> if dataBack:
> #process data
> 
> That would be an infinite loop, and would rely on update returning
> None or similar if no update had occurred.
> 
> I hope that helps in a small way.
> 
> Regards,
> 
> Liam Clarke
>  
> On Tue, 30 Nov 2004 14:04:45 -0800 (PST), Jeff Peery
> wrote:
> > 
> > hello again, 
> > 
> > I am thoroughly confused. I am using Timer from the threading module. I
> want
> > to check a specific file every few minutes to see if it has been altered
> (I
> > do this by checking the size of the file using stat) if it hasn't then I
> do
> > nothing, if it has then I attempt to read the file, grab all the new
> > nume! rical data and update a graph on my computer. 
> 
> 
> > 
> > this file I am reading holds liquid volumetric flow rate measurement data
> > and the python code I am writing is to be used as a statistical process
> > control. basically I watch the results from the measurements by watching a
> > particular file for updates, when an update occurs I grab the data do some
> > more stats, then update my graphs that show on my desktop. 
> > 
> > the timer I used is a class, so I defined a new object (I think thats the
> > right word) as: 
> > 
> > myTimer = Timer(30.0, Update) 
> > 
> > where the timer runs every 30 seconds and Update is a function that checks
> > if the file has been altered and if so then it updates my graphs. I then
> > start the timer: 
> > 
> > myTimer.start() 
> > 
> > I am confused by two things: 
> > 1) I want my timer to restart every 30 seconds. as it shows above it will
> go
> > just ! one time. If I put this in a while loop, will the while loop loop
> > through the start command faster than 30 second intervals or will it wait
> > for the timer to execute the Update function before calling timer.start()
> > again? 
> > 2) I am also confused about how variables are handled when I have multiple
> > and nested functions. for example, I would like to initiate the Update
> > function every 30 seconds and I want the Update funtion to return any new
> > data. how can I return something from a function when the function is
> called
> > from a timer? I cannot do an assignment statement from within the
> > definnition of myTimer? or do I even need to return the data? if I create
> a
> > variable in the update function is it available in my main function? I am
> > not sure how python handles variables that are defined in different
> > functions. 
> > 
> > thank you very much for spending the time to help me, I appreciate it!! 
> > 
> > Jeff 
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> > 
> > 
> 
> 
> -- 
> 'There is only one basic human right, and that is to do as you damn well
> please.
> And with it comes the only basic human duty, to take the consequences.
>  
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list