~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Doc/library/stdtypes.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
446
446
   A right shift by *n* bits is equivalent to division by ``pow(2, n)``.
447
447
 
448
448
 
 
449
Additional Methods on Float
 
450
---------------------------
 
451
 
 
452
The float type has some additional methods.
 
453
 
 
454
.. method:: float.as_integer_ratio()
 
455
 
 
456
    Return a pair of integers whose ratio is exactly equal to the
 
457
    original float and with a positive denominator.  Raises
 
458
    :exc:`OverflowError` on infinities and a :exc:`ValueError` on
 
459
    NaNs.
 
460
    
 
461
    .. versionadded:: 2.6
 
462
 
 
463
Two methods support conversion to
 
464
and from hexadecimal strings.  Since Python's floats are stored
 
465
internally as binary numbers, converting a float to or from a
 
466
*decimal* string usually involves a small rounding error.  In
 
467
contrast, hexadecimal strings allow exact representation and
 
468
specification of floating-point numbers.  This can be useful when
 
469
debugging, and in numerical work.
 
470
 
 
471
 
 
472
.. method:: float.hex()
 
473
 
 
474
   Return a representation of a floating-point number as a hexadecimal
 
475
   string.  For finite floating-point numbers, this representation
 
476
   will always include a leading ``0x`` and a trailing ``p`` and
 
477
   exponent.
 
478
 
 
479
   .. versionadded:: 2.6
 
480
 
 
481
 
 
482
.. method:: float.fromhex(s)
 
483
 
 
484
   Class method to return the float represented by a hexadecimal
 
485
   string *s*.  The string *s* may have leading and trailing
 
486
   whitespace.
 
487
 
 
488
   .. versionadded:: 2.6
 
489
 
 
490
 
 
491
Note that :meth:`float.hex` is an instance method, while
 
492
:meth:`float.fromhex` is a class method.
 
493
 
 
494
A hexadecimal string takes the form::
 
495
 
 
496
   [sign] ['0x'] integer ['.' fraction] ['p' exponent]
 
497
 
 
498
where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
 
499
and ``fraction`` are strings of hexadecimal digits, and ``exponent``
 
500
is a decimal integer with an optional leading sign.  Case is not
 
501
significant, and there must be at least one hexadecimal digit in
 
502
either the integer or the fraction.  This syntax is similar to the
 
503
syntax specified in section 6.4.4.2 of the C99 standard, and also to
 
504
the syntax used in Java 1.5 onwards.  In particular, the output of
 
505
:meth:`float.hex` is usable as a hexadecimal floating-point literal in
 
506
C or Java code, and hexadecimal strings produced by C's ``%a`` format
 
507
character or Java's ``Double.toHexString`` are accepted by
 
508
:meth:`float.fromhex`.
 
509
 
 
510
 
 
511
Note that the exponent is written in decimal rather than hexadecimal,
 
512
and that it gives the power of 2 by which to multiply the coefficient.
 
513
For example, the hexadecimal string ``0x3.a7p10`` represents the
 
514
floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
 
515
``3740.0``::
 
516
 
 
517
   >>> float.fromhex('0x3.a7p10')
 
518
   3740.0
 
519
 
 
520
 
 
521
Applying the reverse conversion to ``3740.0`` gives a different
 
522
hexadecimal string representing the same number::
 
523
 
 
524
   >>> float.hex(3740.0)
 
525
   '0x1.d380000000000p+11'
 
526
 
 
527
 
449
528
.. _typeiter:
450
529
 
451
530
Iterator Types
1543
1622
   .. method:: isdisjoint(other)
1544
1623
 
1545
1624
      Return True if the set has no elements in common with *other*.  Sets are
1546
 
      disjoint if and only if their interesection is the empty set.
 
1625
      disjoint if and only if their intersection is the empty set.
1547
1626
 
1548
1627
      .. versionadded:: 2.6
1549
1628
 
1575
1654
      .. versionchanged:: 2.6
1576
1655
         Accepts multiple input iterables.
1577
1656
 
1578
 
   .. method:: intersection(other)
1579
 
               set & other
 
1657
   .. method:: intersection(other, ...)
 
1658
               set & other & ...
1580
1659
 
1581
1660
      Return a new set with elements common to both sets.
1582
1661
 
1583
 
   .. method:: difference(other)
1584
 
               set - other
1585
 
 
1586
 
      Return a new set with elements in the set that are not in *other*.
 
1662
      .. versionchanged:: 2.6
 
1663
         Accepts multiple input iterables.
 
1664
 
 
1665
   .. method:: difference(other, ...)
 
1666
               set - other - ...
 
1667
 
 
1668
      Return a new set with elements in the set that are not in the others.
 
1669
 
 
1670
      .. versionchanged:: 2.6
 
1671
         Accepts multiple input iterables.
1587
1672
 
1588
1673
   .. method:: symmetric_difference(other)
1589
1674
               set ^ other
1639
1724
      .. versionchanged:: 2.6
1640
1725
         Accepts multiple input iterables.
1641
1726
 
1642
 
   .. method:: intersection_update(other)
1643
 
               set &= other
 
1727
   .. method:: intersection_update(other, ...)
 
1728
               set &= other & ...
1644
1729
 
1645
1730
      Update the set, keeping only elements found in it and *other*.
1646
1731
 
1647
 
   .. method:: difference_update(other)
1648
 
               set -= other
1649
 
 
1650
 
      Update the set, removing elements found in *other*.
 
1732
      .. versionchanged:: 2.6
 
1733
         Accepts multiple input iterables.
 
1734
 
 
1735
   .. method:: difference_update(other, ...)
 
1736
               set -= other | ...
 
1737
 
 
1738
      Update the set, removing elements found in others.
 
1739
 
 
1740
      .. versionchanged:: 2.6
 
1741
         Accepts multiple input iterables.
1651
1742
 
1652
1743
   .. method:: symmetric_difference_update(other)
1653
1744
               set ^= other
1959
2050
   the :keyword:`with` statement.  For example, the following code will
1960
2051
   automatically close *f* when the :keyword:`with` block is exited::
1961
2052
 
1962
 
      from __future__ import with_statement
 
2053
      from __future__ import with_statement # This isn't required in Python 2.6
1963
2054
 
1964
2055
      with open("hello.txt") as f:
1965
2056
          for line in f:
2043
2134
   files, like ttys, it makes sense to continue reading after an EOF is hit.)  Note
2044
2135
   that this method may call the underlying C function :cfunc:`fread` more than
2045
2136
   once in an effort to acquire as close to *size* bytes as possible. Also note
2046
 
   that when in non-blocking mode, less data than what was requested may be
 
2137
   that when in non-blocking mode, less data than was requested may be
2047
2138
   returned, even if no *size* parameter was given.
2048
2139
 
 
2140
   .. note::
 
2141
      This function is simply a wrapper for the underlying
 
2142
      :cfunc:`fread` C function, and will behave the same in corner cases,
 
2143
      such as whether the EOF value is cached.
 
2144
 
2049
2145
 
2050
2146
.. method:: file.readline([size])
2051
2147
 
2173
2269
 
2174
2270
.. attribute:: file.errors
2175
2271
 
2176
 
   The Unicode error handler used to along with the encoding.
 
2272
   The Unicode error handler used along with the encoding.
2177
2273
 
2178
2274
   .. versionadded:: 2.6
2179
2275