Notice: While JavaScript is not essential for this website, your interaction with the content will be limited. Please turn JavaScript on for the full experience.
...divmod() operations, calling divmod() on the quotient from the previous step, with varying divisors. The most common case is probably converting a number of seconds into weeks, days, hours, minutes and seconds. This would today be written as: def secs_to_wdhms(seconds): m, s = divmod(seconds, 60) h, m = divmod(m, 60) d, h = divmod(h, 24) w, d = divmod(d, 7) return (w, d, h, m, s) This is tedious and easy to get wrong each time you need it. If instead the divmod() built-in ...
...divmod(ns_sum, 10 ** 9)) def __sub__(self, other): if not isinstance(other, timespec): raise TypeError ns_diff = self.total_nanoseconds() - other.total_nanoseconds() return timespec(*divmod(ns_diff, 10 ** 9)) def __str__(self): if self.sec < 0 and self.nsec: sec = abs(1 + self.sec) nsec = 10**9 - self.nsec return '-%i.%09u' % (sec, nsec) else: return '%i.%09u' % (self.sec, self.nsec) ...
...divmod__(self, other): """The pair (self // other, self % other). Sometimes this can be computed faster than the pair of operations. """ return (self // other, self % other) def __rdivmod__(self, other): """The pair (self // other, self % other). Sometimes this can be computed faster than the pair of operations. """ return (other // self, other % self) @abstractmethod def __floordiv__(self, other): ...
...divmod(x, y), x/y, x%y, and -x. (The last four can only overflow when the value -sys.maxint-1 is involved.) Currently, x<<n can lose bits for short ints. This will be changed to return a long int containing all the shifted-out bits, if returning a short int would lose bits (where changing sign is considered a special case of losing bits). Currently, hex and oct literals for short ints may specify negative values; for example 0xffffffff == -1 on a 32-bit machine. This will be changed to ...
...divmod) and comparison (==, !=, <, >, <=, >=, cmp) operators in the following cases (check Implicit Construction to see what types could OtherType be, and what happens in each case): Decimal op Decimal Decimal op otherType otherType op Decimal Decimal op= Decimal Decimal op= otherType Decimal should support unary operators (-, +, abs). repr() should round trip, meaning that: m = Decimal(...) m == eval(repr(m)) Decimal should be immutable. Decimal should support the built-in m...
...divmod(x, y) (PEP 228) for int division. Especially the latter is best hidden inside a function. You may also write float(x)/y for true division if you are sure that you don't expect complex numbers. If you know your integers are never negative, you can use int(x/y) -- while the documentation of int() says that int() can round or truncate depending on the C implementation, we know of no C implementation that doesn't truncate, and we're going to change the spec for int() to promise truncation....
...divmod() for Multiple Divisors Bellman SW 304 Controlling Generation of Bytecode Files Montanaro IW 306 How to Change Python's Grammar Hudson, Diederich, Coghlan, Peterson SR 310 Reliable Acquisition/Release Pairs Hudson, Moore SR 313 Adding Roman Numeral Literals to Python Meyer SR 315 Enhanced While Loop Hettinger, Carroll SR 317 Eliminate Implicit Exception Instantiation Taschuk SR 319 Python Synchronize/Asynchronize Block Pelletier SW 321 Date/Time Parsing and Formatting Kuchling SR...
...divmod() and the // and % operators, but these make no sense. Since this was documented, they're being deprecated now. String and unicode methods lstrip(), rstrip() and strip() now take an optional argument that specifies the characters to strip. For example, "Foo!!!?!?!?".rstrip("?!") -> "Foo". There's a new dictionary constructor (a class method of the dict class), dict.fromkeys(iterable, value=None). It constructs a dictionary with keys taken from the ite...