objectoriented -?- functional

A

Armin

Walther said:
loving Java (oo)

Don't mind, weirder things have happened

http://wiki.muenster.org/index.php/Schwan

LOL!!
as well as SML (fun) I use to practice both of them
separately.
Now, with Python I would like to combine 'oo.extend()' with 'functional
map':

Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def reverse_(list):

... """list.reverse() returns None; reverse_ returns the reversed
list"""
... list.reverse()
... return list
...
ll = [[11, 'a'], [33, 'b']]
l = ll[:] # make a copy !
l = map(reverse_, l[:]) # make a copy ?
ll.extend(l)
print("ll=", ll)

('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])

But I expected to get ...
('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
... how would that elegantly be achieved with Python ?

Sorry, I cannot infer the pattern. How would you do that at all?

Peter

If you want to reimplement list.extend() (as I understand?) you will have to
subclass it and change the behaviour.
 
W

Walther Neuper

Hi,

loving Java (oo) as well as SML (fun) I use to practice both of them
separately.
Now, with Python I would like to combine 'oo.extend()' with 'functional
map':

Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... """list.reverse() returns None; reverse_ returns the reversed
list"""
.... list.reverse()
.... return list
....
>>> ll = [[11, 'a'], [33, 'b']]
>>> l = ll[:] # make a copy !
>>> l = map(reverse_, l[:]) # make a copy ?
>>> ll.extend(l)
>>> print("ll=", ll)
('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])

But I expected to get ...
('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
.... how would that elegantly be achieved with Python ?

Cheers, Walther

--
------------------------------------------------------------------------
Walther Neuper Mailto: (e-mail address removed)
Institute for Software Technology Tel: +43-(0)316/873-5728
University of Technology Fax: +43-(0)316/873-5706
Graz, Austria Home: www.ist.tugraz.at/neuper
------------------------------------------------------------------------
 
P

Peter Otten

Walther said:
loving Java (oo)

Don't mind, weirder things have happened

http://wiki.muenster.org/index.php/Schwan
as well as SML (fun) I use to practice both of them
separately.
Now, with Python I would like to combine 'oo.extend()' with 'functional
map':

Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.... """list.reverse() returns None; reverse_ returns the reversed
list"""
... list.reverse()
... return list
...
ll = [[11, 'a'], [33, 'b']]
l = ll[:] # make a copy !
l = map(reverse_, l[:]) # make a copy ?
ll.extend(l)
print("ll=", ll)
('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])

But I expected to get ...
('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
... how would that elegantly be achieved with Python ?

Sorry, I cannot infer the pattern. How would you do that at all?

Peter
 
P

Piet van Oostrum

Walther Neuper said:
WN> Hi,
WN> loving Java (oo) as well as SML (fun) I use to practice both of them
WN> separately.
WN> Now, with Python I would like to combine 'oo.extend()' with 'functional
WN> map':
WN> Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
WN> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
WN> Type "help", "copyright", "credits" or "license" for more information.WN> ... """list.reverse() returns None; reverse_ returns the reversed
WN> list"""
WN> ... list.reverse()
WN> ... return list
WN> ...
ll = [[11, 'a'], [33, 'b']]
l = ll[:] # make a copy !
l = map(reverse_, l[:]) # make a copy ?
ll.extend(l)
print("ll=", ll)
WN> ('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])
WN> But I expected to get ...
WN> ('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
WN> ... how would that elegantly be achieved with Python ?

I guess you later changed 22 to 'a' and 44 to 'b'.

You have made a copy of the outer list with ll[:] (actually twice) but
you have not made copies of the inner lists and these are the important
ones. So the reverse will reverse the *original* inner lists which are
both in ll and in l.

If you still want to do destructive reverses on the inner list you
should make a deep copy:
import copy
l = copy.deepcopy(ll)
l [[11, 'a'], [33, 'b']]
ll.extend(l)
ll
[[11, 'a'], [33, 'b'], ['a', 11], ['b', 33]]

But IMHO it would be better to use non-destructive reverses:
ll [[11, 'a'], [33, 'b']]
l = [list(reversed(x)) for x in ll]
l [['a', 11], ['b', 33]]
ll.append(l)
ll
[[11, 'a'], [33, 'b'], [['a', 11], ['b', 33]]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top