Book Excerpt: The Python Standard Library by Example
Acquiring Function Properties
The partial object does not have __name__ or __doc__ attributes by default, and without those attributes, decorated functions are more difficult to debug. Using update_wrapper() copies or adds attributes from the original function to the partial object.
import functools
def myfunc(a, b=2):
"""Docstring for myfunc()."""
print ’ called myfunc with:’, (a, b)
return
def show_details(name, f):
"""Show details of a callable object."""
print ’%s:’ % name
print ’ object:’,f
print ’ __name__:’,
try:
print f.__name__
except AttributeError:
print ’(no __name__)’
print ’ __doc__’, repr(f.__doc__)
print
return
show_details(’myfunc’, myfunc)
p1 = functools.partial(myfunc, b=4)
show_details(’raw wrapper’, p1)
print ’Updating wrapper:’
print ’ assign:’, functools.WRAPPER_ASSIGNMENTS
print ’ update:’, functools.WRAPPER_UPDATES
print
functools.update_wrapper(p1, myfunc)
show_details(’updated wrapper’, p1)
The attributes added to the wrapper are defined in WRAPPER_ASSIGNMENTS, while WRAPPER_UPDATES lists values to be modified.
$ python functools_update_wrapper.py myfunc: object: <function myfunc at 0x100da2050> __name__: myfunc __doc__ ’Docstring for myfunc().’ raw wrapper: object: <functools.partial object at 0x100d993c0> __name__: (no __name__) __doc__ ’partial(func, *args, **keywords) -new function with parti al application\n of the given arguments and keywords.\n’ Updating wrapper: assign: (’__module__’, ’__name__’, ’__doc__’) update: (’__dict__’,) updated wrapper: object: <functools.partial object at 0x100d993c0> __name__: myfunc __doc__ ’Docstring for myfunc().’
Other Callables
Partials work with any callable object, not just with stand-alone functions.
import functools
class MyClass(object):
"""Demonstration class for functools"""
def method1(self, a, b=2):
"""Docstring for method1()."""
print ’ called method1 with:’, (self, a, b)
return
def method2(self, c, d=5):
"""Docstring for method2"""
print ’ called method2 with:’, (self, c, d)
return
wrapped_method2 = functools.partial(method2, ’wrapped c’)
functools.update_wrapper(wrapped_method2, method2)
def __call__(self, e, f=6):
"""Docstring for MyClass.__call__"""
print ’ called object with:’, (self, e, f)
return
def show_details(name, f):
"""Show details of a callable object."""
print ’%s:’ % name
print ’ object:’,f
print ’ __name__:’,
try:
print f.__name__
except AttributeError:
print ’(no __name__)’
print ’ __doc__’, repr(f.__doc__)
return
o = MyClass()
show_details(’method1 straight’, o.method1)
o.method1(’no default for a’, b=3)
print
p1 = functools.partial(o.method1, b=4)
functools.update_wrapper(p1, o.method1)
show_details(’method1 wrapper’, p1)
p1(’a goes here’)
print
show_details(’method2’, o.method2)
o.method2(’no default for c’, d=6)
print
show_details(’wrapped method2’, o.wrapped_method2)
o.wrapped_method2(’no default for c’, d=6)
print
show_details(’instance’, o)
o(’no default for e’)
print
p2 = functools.partial(o, f=7)
show_details(’instance wrapper’, p2)
p2(’e goes here’)
This example creates partials from an instance and methods of an instance.
$ python functools_method.py
method1 straight:
object: <bound method MyClass.method1 of <__main__.MyClass object
at 0x100da3550>>
__name__: method1
__doc__ ’Docstring for method1().’
called method1 with: (<__main__.MyClass object at 0x100da3550>, ’n
o default for a’, 3)
method1 wrapper:
object: <functools.partial object at 0x100d99470>
__name__: method1
__doc__ ’Docstring for method1().’
called method1 with: (<__main__.MyClass object at 0x100da3550>, ’a
goes here’, 4)
method2:
object: <bound method MyClass.method2 of <__main__.MyClass object
at 0x100da3550>>
__name__: method2
__doc__ ’Docstring for method2’
called method2 with: (<__main__.MyClass object at 0x100da3550>, ’n
o default for c’, 6)
wrapped method2:
object: <functools.partial object at 0x100d993c0>
__name__: method2
__doc__ ’Docstring for method2’
called method2 with: (’wrapped c’, ’no default for c’, 6)
instance:
object: <__main__.MyClass object at 0x100da3550>
__name__: (no __name__)
__doc__ ’Demonstration class for functools’
called object with: (<__main__.MyClass object at 0x100da3550>, ’no
default for e’, 6)
instance wrapper:
object: <functools.partial object at 0x100d994c8>
__name__: (no __name__)
__doc__ ’partial(func, *args, **keywords) -new function with part
ial application\n of the given arguments and keywords.\n’
called object with: (<__main__.MyClass object at 0x100da3550>, ’e
goes here’, 7)
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)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- What's the tweeting protocol?
- New Products
- Trying to Tame the Tablet
- Dart: a New Web Programming Experience
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.



17 min 39 sec ago
17 hours 5 min ago
19 hours 38 min ago
20 hours 55 min ago
21 hours 30 min ago
21 hours 53 min ago
1 day 2 hours ago
1 day 3 hours ago
1 day 5 hours ago
1 day 6 hours ago