Return to Snippet

Revision: 6994
at June 30, 2008 10:21 by ishikawa


Updated Code
def _mangle_name(self, name):
    """
    Any identifier of the form __spam (at least two leading underscores,
    at most one trailing underscore) is textually replaced with
    _classname__spam, where classname is the current class name with
    leading underscore(s) stripped.

    Truncation may occur when the mangled name would be longer than
    255 characters. When the class name consists of only underscores,
    no mangling occurs.
    """

    klass = self.__class__.__name__
    if name.startswith("__") and not name.endswith("__"):
        import re
        if not re.compile(r"^_+$").match(klass):
            # NOTE: In Python 2.5, no trunction occurred?
            #name = ("_%s%s" % (klass, name))[0:255]
            name = ("_%s%s" % (klass, name))
    return name

Revision: 6993
at June 30, 2008 10:20 by ishikawa


Updated Code
"""This module provides ``weakref`` related extentions"""

import __builtin__
import weakref


def _mangle_name(self, name):
    """
    Any identifier of the form __spam (at least two leading underscores,
    at most one trailing underscore) is textually replaced with
    _classname__spam, where classname is the current class name with
    leading underscore(s) stripped.

    Truncation may occur when the mangled name would be longer than
    255 characters. When the class name consists of only underscores,
    no mangling occurs.
    """

    klass = self.__class__.__name__
    if name.startswith("__") and not name.endswith("__"):
        import re
        if not re.compile(r"^_+$").match(klass):
            # NOTE: In Python 2.5, no trunction occurred?
            #name = ("_%s%s" % (klass, name))[0:255]
            name = ("_%s%s" % (klass, name))
    return name


def getter(name):
    """
    Returns getter function for weak referenced attribute.
    """

    def _getter(self):
        wr = self.__dict__.get(_mangle_name(self, name))
        return wr() if wr is not None else None

    return _getter


def setter(name):
    """
    Returns setter function for weak referenced attribute.
    """

    def _setter(self, obj):
        value = weakref.ref(obj) if obj is not None else None
        self.__dict__[_mangle_name(self, name)] = value

    return _setter


def deleter(name):
    """
    Returns deleter function for weak referenced attribute.
    """

    def _deleter(self):
        if _mangle_name(name) in self.__dict__:
            del self.__dict__[_mangle_name(self, name)]


def property(name):
    return __builtin__.property(getter(name), setter(name), deleter(name))

Revision: 6992
at June 30, 2008 10:08 by ishikawa


Initial Code
def _mangle_name(self, name):
    """
    Any identifier of the form __spam (at least two leading underscores,
    at most one trailing underscore) is textually replaced with
    _classname__spam, where classname is the current class name with
    leading underscore(s) stripped.

    Truncation may occur when the mangled name would be longer than
    255 characters. When the class name consists of only underscores,
    no mangling occurs.
    """

    klass = self.__class__.__name__
    if name.startswith("__") and not name.endswith("__"):
        import re
        if not re.compile(r"^_+$").match(klass):
            name = ("_%s%s" % (klass, name))[0:255]
    return name

Initial URL


Initial Description
Python: Private Variables
http://docs.python.org/tut/node11.html#SECTION0011600000000000000000

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.

Truncation may occur when the mangled name would be longer than 255 characters. When the class name consists of only underscores, no mangling occurs.

Initial Title
Name Mangling of Python Private Variables

Initial Tags
python

Initial Language
Python