Pass by reference?

Dale Strickland-Clark dale at out-think.NOSPAMco.uk
Thu May 25 10:08:22 CEST 2000


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?

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?

Thanks for any insight into this.

--
Dale Strickland-Clark
Out-Think Ltd, UK
Business Technology Consultants



Shae Erisson <shapr at uab.edu> wrote in message
news:392C683E.AF7398D4 at uab.edu...
> Dale Strickland-Clark wrote:
> >
> > How do I pass a value by reference so the called routine can update it?
> >
> > Here's an extract from a routine that uses ADO on NT to access a
database.
> > ADO will return the number of records deleted in the second parameter
but
> > it's not working like this.
> >
> >     DBCon.Execute("delete from parts where path='%s' and drive='%s'" %
> > (drive, path), delcnt)
>
> That's not enough information for me to figure out what's wrong... can
> you post five to ten lines of code and what you think they should do?
>
> for every case I've ever seen, pass by reference is the norm in python.
> you can explicity make a copy of a variable if you like though.
>
> here's a quick demo using the 'is' operator:
>
> class TestClass():
>     def update(self, val):
>         self.strx = val
>
> >>> testy = TestClass()
> >>> x = 4
> >>> testy.update(x)
> >>> x is testy.strx
> 1
>
> The 'is' operator returns 1 (aka true) if the two references point to
> the same thing, which means they 'is' the same thing.
>
>
> I've heard that the Redneck version of Python (they read Py3K as PyKKK)
> returns "ain't" for false.
>
> I-have-the-right-to-make-fun-of-my-home-state!'ly y'rs
> --
> sHae mAtijs eRisson (sHae at wEbwitchEs.coM) gEnius fOr hIre
> Control - 9 out of 10 Freaks prefer it.
>





More information about the Python-list mailing list