Hungarian Notation

Lie Lie.1296 at gmail.com
Fri May 30 04:31:03 EDT 2008


On May 27, 12:28 pm, "inhahe" <inh... at gmail.com> wrote:
> Does anybody know of a list for canonical prefixes to use for hungarian
> notation in Python?  Not that I plan to name all my variables with hungarian
> notation, but just for when it's appropriate.

If it was me, I'd use an empty-defined class:

class Fake(object):
    pass

data = 'headinfo=trash;headtrash=info'
Header = Fake()
Header.Str = data
Header.Dict = parse(data)

it saves name if it's important (alternatively, you may also use a
dict or a tuple/list to store the string/dict pair).
But using Fake class just like that is difficult to work with if I
need to "write" to the data (not read only) and synchronizes the data,
in that case, it's easy to extend the Fake Class:

class Fake(object):
    def __init__(self, data):
        self.data = parse(data)

    def toStr(self):
        return str(self.data)
    def fromStr(self, s):
        self.data = parse(s)
    Str = property(toStr, fromStr)

    def toDict(self):
        return self.data
    def fromDict(self, s):
        self.data = s
    Dict = property(toDict, fromDict)

you might go as far as overriding __str__ and __repr__ as appropriate.



More information about the Python-list mailing list