Edited Mon, 03 Dec 2007 22:48
While metaclasses are not too hard to understand (here, here, here, here, and here), the way there are implemented may seem sometimes using black magic. The following documented example should help to follow the different steps and calls.
class Meta(type):
'''
Metaclass
'''
def __new__(mcl, name, bases, dic):
'''
Overrides the default construction of the class T (classobject).
(__new__ is a static method)
'''
# Equivalent to something like this:
# T = Meta('T', (), {...})
print 'Meta::__new__'
# insert your personalization here ...
return type.__new__(mcl, name, bases, dic)
def __call__(cls, *a, **k):
'''
Say T is an instance of Meta, thus on T(), this method will
be called.
'''
print 'Meta::__call__'
# The next call typically implements something like this:
#
# def __call__(cls,*a,**k):
# nu = cls.__new__(cls,*a,**k)
# if isinstance(nu, cls):
# cls.__init__(nu,*a,**k)
# return nu
return super(Meta, cls).__call__(*a, **k)
class T:
__metaclass__ = Meta
def __new__(cls, *p, **k):
"""
Builds the T's instance, delegated from Meta.__call__.
"""
# if this method doesn't return an instance of T, then
# __init__ will not be called.
print 'T::__new__'
return super(T, cls).__new__(cls, *p, **k)
def __init__(self):
"""
Initializes the class' instance, delegated from Meta.__call__.
"""
print 'T::__init__'
print type(T);T();T()
You can observe that the two __new__(...) methods despite the fact they share the same name and builds a new object, there are really really differents. It gives:
Meta::__new__ <class '__main__.meta'> Meta::__call__ T::__new__ T::__init__ Meta::__call__ T::__new__ T::__init__
Edited Sat, 16 Sep 2006 23:26
Excerpt from this link:
the for statement calls iter() on the container object. The function returns an iterator object that defines the method next() which accesses elements in the container one at a time. When there are no more elements, next() raises a StopIteration exception which tells the for loop to terminate.
Edited Wed, 04 Jul 2007 21:38
- prefer iterate over tuple than over list: i in (a, b ,c) than i in [a, b, c]. tuples are immutable sequences.
- beware of iterator invalidation when deleting an element, prefer iterating over list copy or wait until P3k
- use .get(x, default) for retrieving a value associated to x
-
l = [1,2,3] for accessing the last element, prefer: l[-1] than: l[len(l)-1]
-
>>> a = [1,2,3] >>> b = a >>> b [1, 2, 3] >>> a[:] = [42] >>> b [42] >>> a = [24] >>> b [42] >>>
Edited Wed, 04 Jul 2007 21:44
This pep and this blog entry can give an idea of the planned changes in Python 3.0, there is also a dedicated mailing list. At the end of this other pep there is a partial list of main changes.
- Rip out reduce()
- .has_key() changed to x in dict / dict.contains
Edited Thu, 22 May 2008 16:02
- Python 2.5
- Dedicated pep listing all the majors modifications.
- Dedicated pep for code modernization.
Edited Wed, 04 Jul 2007 21:46
One other useful page is a comprehensive list of commons terms, used by Python developpers.
Edited Wed, 04 Jul 2007 21:42
Edited Sat, 30 Sep 2006 08:32
for date handling there is two useful functions available in this module :
- formatdate(): format the current date according to the rfc822. Respecting this standart let you generate valid rss feeds for example.
- parsedate(): which is able to parse date strings, of course, these strings must be rfc822 compliants, for instance parsedate(formatdate()) works and returns a tuple.
Edited Mon, 03 Dec 2007 22:52
here is a list of decorators, it can shows how to design a well implemented decorator.
Edited Tue, 04 Dec 2007 00:57
Ultimate reference about object model of Python: here.
Obvioulsy this page contains only sparse and random insights, therefore please consult other ressources for more details.