newbie questions

houbahop d.lapasset
Sat Dec 11 15:19:40 EST 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 :D
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" <steven.bethard at gmail.com> a écrit dans le message de news: 
M9Iud.643188$mD.7203 at 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-effect_(computer_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". 





More information about the Python-list mailing list