Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie questions

Reply
Thread Tools

newbie questions

 
 
houbahop
Guest
Posts: n/a
 
      12-11-2004
Hello again everyone ,
var2[:]=[] has solved my problem, and I don't understand why it is
programming by side effect.
I don't think it's bad, look at this, it's what I've done :

def Clear(lvar)
lvar[:]=[]

def main (starting class)
var1=[]
var1.append('a')
Clear(var1)

var1 can only be modified by Clean(), so, if I need to debug var1, I
quickly know that I must look in Clean()


For me programming by side effect is that :

def Clear(lvar)
var1=[]

def main (starting class)
global var1=[]
var2=[]
var1.append('a')
Something(var2)


I am wrong?
Dominique.


 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      12-11-2004
houbahop wrote:
> Passing a pointer by value appears to me as passing a var by reference.


Well, at least in the PL literature, there's a subtle difference. If
Python supported pass-by-reference, you could do something like:

>>> def clear(byref lst):

.... lst = []
....
>>> x = [pow(x, 11, 17) for x in range(10)]
>>> clear(x)
>>> x

[]

Passing a variable by reference means that the *variable* in the
function is the same *variable* as outside the function. Notice that
this is not the same thing as saying that the variable in the function
is a pointer to the same object as the variable outside the function.

Python doesn't support pass-by-reference in this standard use of the
term. But Python is basically *only* passing pointers around; there is
no way to do anything else. So if passing a pointer by value is
sufficient for your purposes then you won't have any problems with
Python, because this is what it always does. =)

> Thanks I will try all of that, but what does really means mutating in
> python? It's the first time I hear this word in programming )


The term is used most commonly in the pair accessor/mutator, also known
as getter/setter. In general, a mutator is just a method of an object
that changes that object's state. In Python, assignment does not invoke
a method of an object; it binds a name to an object. For this reason,
code like:

def clear(lst):
lst = [] # binds the name lst to a new value, []
# ignoring previous value

will not change the list because it is not calling methods of the list
object to change it. It will instead rebind the local name "lst" to a
new object, []. This does not change the previous object that was
associated with "lst" and so if you have another variable bound to the
previous value ("x" in my examples), it will still retain the old,
unchanged object.

So, while assignment does not invoke a method of an object, many other
things in Python do, and all of these things will appropriately modify
the state of the object. For example:

def clear(lst):
lst[:] = [] # implicitly calls the setter/mutator list.__setslice__

def clear(lst):
while lst: # implicitly calls the getter/accessor len(lst)
lst.pop() # explicit call to setter/mutator list.pop

# don't use this; it's inefficient, I'm just using it as an example
# of using a different setter/mutator
def clear(lst):
while lst: # implicitly calls the getter/accessor len(lst)
last_item = lst[-1] # implicitly calls the getter/accessor
# list.__getitem__
lst.remove(last_item) # explicitly calls setter/mutator
# list.remove

Note that all of these functions will successfully clear the list
because they mutate the object itself, instead of simply rebinding a
name that at one point had been associated with the object.

HTH,

STeve
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      12-11-2004
houbahop wrote:
> Hello again everyone ,
> var2[:]=[] has solved my problem, and I don't understand why it is
> programming by side effect.
> I don't think it's bad, look at this, it's what I've done :
>
> def Clear(lvar)
> lvar[:]=[]
>
> def main (starting class)
> var1=[]
> var1.append('a')
> Clear(var1)


From http://en.wikipedia.org/wiki/Side-ef...mputer_science)

In computer science, a side-effect is a property of a programming
language function that it modifies some state other than its return value.

Given this definition, I think you're fine -- clearing the list isn't a
side effect of the function; it is the purpose of the function. Hence
the function has no return value.[1]



Of course, in this simple case, I wouldn't be likely to write the clear
function since the inline code is simpler and has less overhead:

def main()
var1 = []
var1.append('a')
var1[:] = []

or, since in this case, you only care about var1, you can just rebind it
(and let Python garbage-collect the old list):

def main()
var1 = []
var1.append('a')
var1 = []

Steve

[1] Technically, all Python functions without a return statement return
None, but for our purposes, we can consider a function like this to have
"no return value".
 
Reply With Quote
 
houbahop
Guest
Posts: n/a
 
      12-11-2004
Thank you Steven,
The funny thing is that I have taken a look at wikipedia just before to be
sure that I wasn't wrong
I agree with you about not making a clear function to just put what I've put
in my sample, but
this sample was more simple that what I really need for explaining purpose.

In reality My clear function is cleaning a dozen of arrays and since I need
to call it several time I have made a function of it.

a few days ago I was trying python for the little script I'm working on and
this language is more powerfull that I was thinking and very pleasant to
code.
I believe I will use it more in the future.
Dominique.


"Steven Bethard" <(E-Mail Removed)> a écrit dans le message de news:
M9Iud.643188$mD.7203@attbi_s02...
> houbahop wrote:
>> Hello again everyone ,
>> var2[:]=[] has solved my problem, and I don't understand why it is
>> programming by side effect.
>> I don't think it's bad, look at this, it's what I've done :
>>
>> def Clear(lvar)
>> lvar[:]=[]
>>
>> def main (starting class)
>> var1=[]
>> var1.append('a')
>> Clear(var1)

>
> From http://en.wikipedia.org/wiki/Side-ef...mputer_science)
>
> In computer science, a side-effect is a property of a programming language
> function that it modifies some state other than its return value.
>
> Given this definition, I think you're fine -- clearing the list isn't a
> side effect of the function; it is the purpose of the function. Hence the
> function has no return value.[1]
>
>
>
> Of course, in this simple case, I wouldn't be likely to write the clear
> function since the inline code is simpler and has less overhead:
>
> def main()
> var1 = []
> var1.append('a')
> var1[:] = []
>
> or, since in this case, you only care about var1, you can just rebind it
> (and let Python garbage-collect the old list):
>
> def main()
> var1 = []
> var1.append('a')
> var1 = []
>
> Steve
>
> [1] Technically, all Python functions without a return statement return
> None, but for our purposes, we can consider a function like this to have
> "no return value".



 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-11-2004

Steven Bethard wrote:
>
> Of course, in this simple case, I wouldn't be likely to write the

clear
> function since the inline code is simpler and has less overhead:
>
> def main()
> var1 = []
> var1.append('a')
> var1[:] = []


Even less overhead: del var1[:]

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-11-2004
On Sat, 11 Dec 2004 12:43:01 GMT, "houbahop" <d.lapasset[Remove
me)@chello.fr> declaimed the following in comp.lang.python:

> Thank you everyone, but I still not understand why such a comon feature like
> passing parameters byref that is present in most serious programming
> languages is not possible in a clean way,here in python.
>

Well, in C, everything is also passed by value. The programmer
has to explicitly pass an "address as the value" instead of the actual
value if they want to change the contents. And, of course, the function
has to know that it is an address, and explicitly dereference the
address to gain access to the actual content value.

Using my overworked and not quite complete example...

Most languages variables can be described as named boxes (think
of a post office sorting rack -- each "person/address" has a separate
box.

In those languages, "A = B" means finding the BOX with belonging
to "B" (has "B" on the front of the box), COPYING what is inside it, and
then putting that COPY into the box belonging to "A" (taking out and
discarding whatever might have been in "A"'s box).

In Python, you still have those boxes (objects), but the label
(person/address) is not engraved on the box. Instead, it is on a
"Post-it" note. "A = B" means finding the "Post-it" note -- where ever
it is, since it isn't a fixed location -- and putting it on the box that
also has the "B" "Post-it" note. If the "A" was the last "Post-it" note
on the original box, the box contents are thrown away, and the box is
available for reuse. You now have ONE BOX with TWO "Post-it"s, "A" and
"B".

When you call a function, passing "A", for example, you create a
third "Post-it" note, with the function's dummy argument name "D", and
attach it to the same box. The box now has three "Post-it"s attached;
two from the outer scope, and one from the function scope.

An assignment to "D" moves the "Post-it" from that shared box,
to whatever box contains the value of the assignment. And when the
function exits, the clean-up code goes through and finds all the
"Post-it"s that belong to the function, and deletes the "Post-it" (the
contents of the box only get deleted if no other "Post-it" is attached).

The closest equivalent to the C, that I can imagine at the
moment, is to package your arguments into a list before the call, invoke
the function passing the list -- and access the arguments by list
subscripts, followed by extracting the list elements back into the
original "names" after the call returns... Untested:

args = [ a1, a2, a3 ]
function(args)
(a1, a2, a3) = tuple(args)

However, 'tis probably cleaner to just use a tuple return from
the function...

(a1, a2, a3) = function(a1, a2, a3)

Where the function can perform internal modifications to the
arguments passed in, and then returns ALL of them at the end. Other
languages can only return a single entity, Python can return the
"world"... (... return globals() <G>)

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
houbahop
Guest
Posts: n/a
 
      12-11-2004

"Dennis Lee Bieber" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> On Sat, 11 Dec 2004 12:43:01 GMT, "houbahop" <d.lapasset[Remove
> me)@chello.fr> declaimed the following in comp.lang.python:
>
>> Thank you everyone, but I still not understand why such a comon feature
>> like
>> passing parameters byref that is present in most serious programming
>> languages is not possible in a clean way,here in python.
>>

> Well, in C, everything is also passed by value. The programmer
> has to explicitly pass an "address as the value" instead of the actual
> value if they want to change the contents. And, of course, the function
> has to know that it is an address, and explicitly dereference the
> address to gain access to the actual content value.
>
> Using my overworked and not quite complete example...
>


Hello and thank you,

To be honnest I must take a moment and translate your example into french to
really understand it (but I believe it's very well explained and I will
understand)
I remember at school when an unix teacher was explaining what was an UID and
a GID, like if the files were two persons talking and asking each other "
what's your UID?, we got the same, so, what is your GID ?", and I never had
problems with UID and GID since that

I have thinked to try to return an array of values like a function does
(instead of passing many params to a sub) but didn't take the time to do it.

I believe that programming in python involves to learn a kind of new
phillosophy, new words too. as an exemple of this, for me
a 'tuple' is related to database stuff. What I call an array seems to be
called a list in python (C programmers use the word 'matrix' when talking of
a two dimentionnal array, and 'vector' when talking of a one dimention
array...sorry, a list ).

Regards,
Dominique.


 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-12-2004
On Sat, 11 Dec 2004 23:56:05 GMT, "houbahop" <d.lapasset[Remove
me)@chello.fr> declaimed the following in comp.lang.python:

>
> I believe that programming in python involves to learn a kind of new
> phillosophy, new words too. as an exemple of this, for me
> a 'tuple' is related to database stuff. What I call an array seems to be


"tuple" gets around... But yes, relational database theory uses
it for what most would call a "row" or "record". So think of a Python
tuple as being a "record" (or C "struct") with the condition that you
can not change the contents of it.

> called a list in python (C programmers use the word 'matrix' when talking of


One difference in view is that most language have bounded arrays
-- you had to allocate how many elements are in the array. Python lists
are dynamic, you can add and remove elements. Also, many languages
restrict all elements of an array to being the same data type. Python
lists can be any of Pythons objects -- and can be mixed.

--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-12-2004
John Machin wrote:

>> Of course, in this simple case, I wouldn't be likely to write the clear
>> function since the inline code is simpler and has less overhead:
>>
>> def main()
>> var1 = []
>> var1.append('a')
>> var1[:] = []

>
> Even less overhead: del var1[:]


even less overhead:

var1 = []

(if you rely on having multiple references to the same list, instead of referring
to the list by name, you may want to reconsider the design)

</F>



 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-12-2004
Fredrik Lundh wrote:
> John Machin wrote:
>
>
>>>Of course, in this simple case, I wouldn't be likely to write the clear
>>>function since the inline code is simpler and has less overhead:
>>>
>>>def main()
>>> var1 = []
>>> var1.append('a')
>>> var1[:] = []

>>
>>Even less overhead: del var1[:]

>
>
> even less overhead:
>
> var1 = []
>
> (if you rely on having multiple references to the same list, instead of referring
> to the list by name, you may want to reconsider the design)


Yes, that's what the next 4 lines of my message said.

Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie with newbie questions JohnE ASP .Net 3 08-17-2009 10:10 PM
Malloc and free questions - learner questions pkirk25 C Programming 50 10-04-2006 02:22 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments