This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Confusing Descrintro example
Type: enhancement Stage:
Components: Documentation Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: arsatiki, benjamin.peterson, georg.brandl, gvanrossum
Priority: low Keywords: easy

Created on 2008-01-24 13:35 by arsatiki, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg61631 - (view) Author: Antti Rasinen (arsatiki) Date: 2008-01-24 13:35
Guido's document "Unifying types and classes in Python 2.2" (descrintro)
contains a confusing example for metaclass newbies. 
<http://www.python.org/download/releases/2.2.3/descrintro/#metaclass_examples>

The example in question is autoprop, which uses the variable "name" in
three different contexts. First, it is the name of the class being
created. Second, it is used in a for loop to denote the keys of the
class dict. And thirdly, it is used to denote substrings of a subset of
those keys.

Upon my first encounter with the example, I found myself staring at the
getattr and setattr lines in disbelief. I associated "name" as the
parameter given to the __init__ function.

I'd propose changing the first for-loop name to "member" or similar
and the second to "propname" or "prop". 

Furthermore, a modern version of the same example could use sets and set
comprehensions instead of dict.keys():
props = {member[5:] for member in dict if member.startswith...}
msg90427 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2009-07-11 16:45
I'm guessing you weren't ready for learning about metaclasses if you
didn't get the fact that 'name' was the loop control variable in the two
different loops.  The example is only 11 lines long!

Still, I'm fine with the two suggested renames.

I'm not fine with rewriting the example using modern constructs, since
it is part of the docs for Python 2.2.  Perhaps the doc is still useful
but then it should be first moved into the regular doc tree and *then*
adapted to modern times (and we can't really release that version until
2.7 and 3.2 are released).

Unfortunately I do not have a checkout of the website handy any more (or
I can't remember where I put it).  Maybe one of the webmasters can make
the suggested fixes?

The fixed code that I agree with is:

class autoprop(type):
    def __init__(cls, name, bases, dict):
	super(autoprop, cls).__init__(name, bases, dict)
	props = {}
	for member in dict.keys():
            if member.startswith("_get_") or member.startswith("_set_"):
		props[member[5:]] = 1
	for prop in props.keys():
            fget = getattr(cls, "_get_%s" % prop, None)
            fset = getattr(cls, "_set_%s" % prop, None)
            setattr(cls, prop, property(fget, fset))
msg90437 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-07-12 00:55
I've now updated the website to Guido's new code.
History
Date User Action Args
2022-04-11 14:56:30adminsetgithub: 46216
2009-07-12 00:55:49benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg90437

resolution: fixed
2009-07-11 16:45:55gvanrossumsetmessages: + msg90427
2009-07-11 10:43:56georg.brandlsetassignee: georg.brandl -> gvanrossum

nosy: + gvanrossum
2009-07-04 02:39:53ezio.melottisetassignee: georg.brandl

nosy: + georg.brandl
2008-01-24 20:39:56christian.heimessetpriority: low
keywords: + easy
versions: - Python 2.5, Python 2.4, Python 2.3, Python 2.2.3, Python 2.2.2, Python 2.2.1, Python 2.2, Python 2.1.2, Python 2.1.1
2008-01-24 13:35:46arsatikicreate