very good reasons?

Grant Griffin g2 at seebelow.org
Fri Sep 29 19:37:27 CEST 2000


Hi Gang,

I was trying to chain "sort" and "reverse" together the other day <<confess>>,
ala Perl, but I found that it didn't work, e.g.:

   Python 2.0b2 (#6, Sep 26 2000, 14:59:21) [MSC 32 bit (Intel)] on win32
   >>> a=[1,2,3,4]
   >>> b=a.sort().reverse()
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   AttributeError: 'None' object has no attribute 'reverse'
   >>> c=a.sort()
   >>> repr(c)
   'None'

I would have expected sort and reverse to return the list in question, but
instead they return None.  So I had to do something like:

   >>> a=[1,2,3,4]
   >>> a.sort()
   >>> a.reverse()
   >>> b=a
   >>> b
   [4, 3, 2, 1]

Likewise, I wondered the other day why "real" and "imag" are "attributes" of
complex numbers instead of functions:

   >>> a=1+2j
   >>> a.imag
   2.0
   >>> a.real
   1.0

Then I realized <head slap!>, that it must be so you could _assign_ to them:
Hey, that sounds kindda useful!  However:

   >>> a.real = 5
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   TypeError: object has read-only attributes
   >>> a.imag = 6
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   TypeError: object has read-only attributes
  
Having faith in our BDFL as I do, I'm sure there are very good reasons for these
things.  Does anybody know what they are?

hoping-for-more-head-slaps-<<endorphins>>-ly y'rs,

=g2

_____________________________________________________________________

Grant R. Griffin                                       g2 at dspguru.com
Publisher of dspGuru                           http://www.dspguru.com
Iowegian International Corporation            http://www.iowegian.com




More information about the Python-list mailing list