Keeping Up with Python: the 2.2 Release
A new divisor operator ( // ) has been created that always truncates the fraction and rounds it to the next smallest whole number toward the left on the number line, regardless of the operands' numeric types. This operator works starting in 2.2 and does not require the __future__ directive above.
>>> 1 // 2 # floors result, returns integer 0 >>> 1.0 // 2.0 # floors result, returns float 0.0 >>> -1 // 2 # move left on number line -1
Without getting into the arguments of this change, the feeling is that perhaps Python's division operator has been flawed since the beginning, especially because Python is a strong choice as a first programming language for people who aren't used to floor division. One of the examples Guido uses in his “What's New in Python 2.2” ZPUG talk is:
def velocity(distance, totalTime):
rate = distance / totalTime
This is bad because this function is not numeric-type-independent.
Your results with a pair of floats certainly differs from that of
sending in a pair of integers. To bridge the dichotomy, you must
resolve the following intransitivity in your head:
>>> 1 == 1.0 1 >>> 2 == 2.0 1 >>> 1 / 2 == 1.0 / 2.0 # classic division 0If you use Python's new model of division, the universe is at peace once again:
>>> from __future__ import division >>> 1 / 2 == 1.0 / 2.0 # true division 1 >>> 1 // 2 == 1.0 // 2.0 # floor division 1While this seems like the proper and right thing to do, one cannot help but be concerned with the code breakage it may lead to. Fortunately, the Python developers have kept this in mind, as this change will not be permanent until Python 3.0, which is still years away. Those who desire the new division can import it or start Python with the -Qnew command-line option. There are a few options to turn on warnings to prepare for the upcoming new division.
You can get more information from PEP 238, but dig through the comp.lang.python archives for the heated debates. Table 2 summarizes the division operators in the various releases of Python and the differences in operation when you import division (from __future__).
Merging Python types and classes has been on the want list for quite a while. Programmers are dismayed to discover that they cannot subclass existing data types, such as a list, to customize for their applications.
To learn more, it can't hurt to look through both the PEPs involved and a tutorial Guido wrote specifically for those who want to get up to speed quickly on the new style classes without having to wade through all the intricate details found in the PEPs (see Resources). We will also give you a teaser class that extends a Python list with enhanced stack features.
This example, stack2.py, is motivated by one of the iterator examples above (see also Example 6.2 at the Core Python Programming web site).
#!/bin/env python
'stack2.py -- subclasses and extends a list'
class Stack(list):
def __init__(self, *args):
list.__init__(self, args) # call base class
# constructor
def push(self, *args):
for eachItem in args: # can push multiple
self.append(eachItem) # items
def pop(self, n=1):
if n == 1: # pop single item
return list.pop(self)
else: # pop multiple items
return [ list.pop(self) for i in range(n) ]
Below is the output we get from flexing our newfound capabilities:
>>> from stack2 import Stack >>> m = Stack(123, 'xyz') >>> m [123, 'xyz'] >>> m.push(4.5) >>> m [123, 'xyz', 4.5] >>> m.push(1+2j, 'abc') >>> m [123, 'xyz', 4.5, (1+2j), 'abc'] >>> m.pop() 'abc' >>> m.pop(3) [(1+2j), 4.5, 'xyz'] >>> m [123]In addition to being able to subclass built-in types, other highlights of the new style classes include:
“Cast” functions being factories.
New __class__, __dict__, and __bases__ attributes.
__getattribute__() Special Method (smarter than __getattr__()).
Class descriptors.
Class properties.
Static methods.
Class methods.
Superclass method calls.
Cooperative methods.
New diamond diagram name resolution.
Fixed set of allowed class attributes with Slots.
For more information on the new style classes and the unification of types and classes, see both PEPs 252 and 253 as well as the aforementioned tutorial by Guido.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- What's the tweeting protocol?
- One Hand Slapping
- New Products
- Readers' Choice Awards
- Trying to Tame the Tablet
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




6 hours 22 min ago
8 hours 55 min ago
10 hours 12 min ago
10 hours 47 min ago
11 hours 9 min ago
15 hours 58 min ago
16 hours 44 min ago
18 hours 18 min ago
19 hours 55 min ago
21 hours 53 min ago