The new Module

(Optional in 1.5.2) The new modules is a low-level module that allows you to create various kinds of internal objects, such as class objects, function objects, and other kinds that are usually created by the Python runtime system. Example 13-7 demonstrates this module.

Note that if you e using 1.5.2, you may have to rebuild Python to use this module; it isn enabled by the default on all platforms. In 2.0 and later, however, its always available.

Example 13-7. Using the new Module
File: new-example-1.py

import new

class Sample:

 a = "default"

 def _ _init_ _(self):
 self.a = "initialised"

 def _ _repr_ _(self):
 return self.a

#
# create instances

a = Sample()
print "normal", "=>", a

b = new.instance(Sample, {})
print "new.instance", "=>", b

b._ _init_ _()
print "after _ _init_ _", "=>", b

c = new.instance(Sample, {"a": "assigned"})
print "new.instance w. dictionary", "=>", c

normal => initialised
new.instance => default
after _ _init_ _ => initialised
new.instance w. dictionary => assigned

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules



Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net