Pass by reference?

Shae Erisson shapr at uab.edu
Thu May 25 11:51:08 CEST 2000


Dale Strickland-Clark wrote:
> 
> There isn't much else that's relevant around this statement.
> 
> DBCon is an ADO connection object. This statement deletes some rows in a
> database and should return the number deleted in delcnt - the second
> argument. However, delcnt is always zero regardless of actual count deleted.
> 
>     delcnt = 0
>     print DBCon.Execute("delete from parts where path='%s' and drive='%s'" %
> (path, drive), delcnt, 1)
>     print delcnt
> 
> However, I stuck a print statement on the front to see what was returned and
> found a list of two items, the second of which is the count I want - but why
> isn't it returned where it should be?

I don't know what code/module etc you're using, but Python's roots are a
bit more towards functional languages than some other languages, that
means that often a call will return its output rather than assigning it
to a status variable you've passed into it.

You may want to try:
x = DBCon.Execute(stuff)
and then read from x to get your delcnt.

> Also the following program proves to me that arguments are passed by value
> and not by reference:
> 
> def wibble(var):
>     var = 1
> 
> x = 0
> wibble(x)
> print x
> 
> It prints 0 showing that the assignment in wibble is made to a copy of x
> 
> So - how do I pass a variable to a function/subroutine/method by reference?

try this:
def wibble(var):
    print 'var value before is', var
    print 'do var and x have exactly the same address in memory?',x is
var
    var = 2
    print 'var value after is', var
    print 'do var and x have exactly the same address in memory?',x is
var

x = 0
wibble(x)
print x

what this means is that you're making a new reference named var inside
the function, rather than changing the variable in the scope 'above' the
function.
-- 
sHae mAtijs eRisson (sHae at wEbwitchEs.coM) gEnius fOr hIre
   bRing mE fIve sQuirrels aNd nO oNe wIll gEt hUrt
13:00pm up 1 season, 1 squirrel, load average: 1+1j 0.5+2j 0.2+0.5j



More information about the Python-list mailing list