Have been programming at work, and rediscovering the joys of programming quirks. Specifically, Python quirks.
Well, you've probably seen that one before. I ended up writing a program which depended on that property to function properly. It was recursive, and passed round a list as a parameter, which needed to be altered by various instances of the function. Then just as I was about to fire it up, I realised that in one case I didn't need to alter just the end of the list, but change it completely. As a hack, I ended up writing this function:
I feel dirty.
On a completely unrelated note: not sure whether I want to go out to the pub tomorrow (oops! today now!) or just stay in. Anybody else going to be there?
def foo1(): x = ['data'] bar1( x ) print x def bar1( liszt ): liszt[0] = 'spock' >>> foo1() ['spock'] def foo2(): x = ['data'] bar2( x ) print x def bar2( liszt ): liszt = ['spock'] >>> foo2() ['data']
Well, you've probably seen that one before. I ended up writing a program which depended on that property to function properly. It was recursive, and passed round a list as a parameter, which needed to be altered by various instances of the function. Then just as I was about to fire it up, I realised that in one case I didn't need to alter just the end of the list, but change it completely. As a hack, I ended up writing this function:
def replacelist( list1, list2 ): """ Replace the data in list 1 with that of list 2, while maintaining the original list pointer """ while list1: del list1[0] for item in list2: list1.append( item )
I feel dirty.
On a completely unrelated note: not sure whether I want to go out to the pub tomorrow (oops! today now!) or just stay in. Anybody else going to be there?
Tags:
From:
no subject
There must be nicer ways of doing this, though I don't really know python well enough to spot them.
From:
no subject
The functional approach:
Make the list a return value. My function was already returning a string, but as Python allows you to return a tuple, I could have altered it like so:
return (returnstr, liszt)
But I couldn't be bothered.
The object-oriented approach:
Create a simple holder for the list:
class listholder: def __init__( self, liszt ): self.liszt = lisztAnd then just use a combination of l = listholder( data ) and l.liszt = newdata. The object reference will be preserved.
From:
no subject
class Array def replace(newArray) clear newArray.each{|elem| self << elem} end endWhich adds a replace method to the Array class(which are very similar to python lists) and both alters and returns the object itself.
But then that's ruby...