On Thursday 22 September 2005 23:46, [EMAIL PROTECTED] wrote: > I am coming to Python from Perl. Does Python have anything like the diamond > operator found in Perl?
The correct answer is not really no, but you can manually perform the same tasks. For those who don't know perl, <> is an incredibly useful little operator that does some really wierd and wonderful things :-) (That said, I don't think I'd like a magic operator like this in python :-) If you want to do this: @a = <>; The equivalent is probably best expressed thus: def diamond_array(): # Note, untested... import sys if len(sys.argv ==1): return sys.stdin.readlines() else: result = [] for file in sys.argv[1:]: try: file = open(file) result.extend(file.readlines()) except IOError: pass return result a = diamond_array() If however you want to do the equivalent of: while ($line = <>) { ... } That mode of <> probably best equates to this: for line in diamond_lines(): .... def diamond_lines(): # Note, untested... import sys if len(sys.argv ==1): for line in sys.stdin.xreadlines(): yield line else: for file in sys.argv[1:]: try: file = open(file) for line in file.xreadlines(): yield line except IOError: pass As for this idiom: while(1) { $line = <>; ... } That probably matches best to this: diamond_iterator = diamond_lines() # same function as above while 1: line = diamond_iterator.next() Whereas this kind of trick: $firstline = <>; @remaining_lines = <>; Would map to this: (using the functions above) diamond_iterator = diamond_lines() firstline = diamond_iterator.next() remaining_lines = list(diamond_iterator.next()) Or to show a relatively common perl idiom: $firstline, @remaining_lines= <>,<>; Maps to: diamond_iterator = diamond_lines() firstline,remaining_lines = diamond_iterator.next(), list(diamond_iterator.next()) Best Regards, Michael. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor