spudtater: (Default)
([personal profile] spudtater May. 25th, 2006 11:53 pm)
Have been programming at work, and rediscovering the joys of programming quirks. Specifically, Python quirks.
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?
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

.

Profile

spudtater: (Default)
spudtater

Most Popular Tags

Powered by Dreamwidth Studios

Style Credit

Expand Cut Tags

No cut tags