[Tutor] class/type methods/functions

spir denis.spir at free.fr
Thu Oct 30 21:09:29 CET 2008


Hello again,

I just tried with the classmethod() built_in function. Well, this is 
exactly what I was looking for. It supplies a way of defining type_level 
methods -- what was missing before.
[I still think this is a workaround: we would not need this if the 
syntax was the same for methods and properties, or what?]
It makes the job: below an simulation example related to the previously 
exposed case.

class Config(object):
    pass
config = Config()

class Format(object):
    @classmethod                    # with so-called "decorator"
    def config(Format):
        Format.a = config.format_a
    #config = classmethod(config)   # with built_in function

def build_config():
    # (re)read config data from file
    # e.g. value for format 'n' parameter in file
    config.format_a = 1
    # let the symbol types (re) import their proper config
    # e.g. Format
    Format.config()    # no error!

build_config()
print Format.a # 1!

Well, would someone clarify the point about decorators? Rather the point 
of having both a built-in function (introduced in python 2.2, as I just 
read) and a "decorator" (2.4) for a single use of declaring a method 
not_to_be_bound_to_an_instance? I'm rather sure there's more behind... I 
noticed that more and more features expressed with built-in functions 
get alternative syntax.


Denis



More information about the Tutor mailing list