* Jean-Michel Pichavant:
Steven D'Aprano wrote:
On Mon, 30 Nov 2009 02:11:12 +0100, Alf P. Steinbach wrote:

I think if one could somehow declare names as const (final, readonly,
whatever) then that would cover the above plus much more.

Having real constants is one feature that I miss. Because Python doesn't have constants, I find I've lost the discipline to avoid "magic numbers" (and strings) in my code.


I don't get you, this can be easily avoid with a strong naming convention. I mean, despite they are not really constants, you can still use variables to name your numbers and string, to give the reader a usefull hint on the meaning of your code. Yet it is still a matter of discipline, it is sometimes very tempting to put the string directly (unlike numbers string can be meaningful sometimes)

It may be surprising how many things turn out to be readonly.

Consider the OP's code:


<code>
class ValueColumn(AbstractColumn):
    def __init__(self, name, header, domain_names):
        if type(domain_names) != tuple:
            raise ValueError('a tuple of domain names must be given')
        for name in domain_names:
            if type(name) != str:
                raise ValueError('a tuple of domain names must be given')
        self.domain_names = domain_names
        super(ValueColumn, self).__init__(name, header)
</code>


Here the arguments should ideally be read only names, which would have cought the bug of reusing 'name' in the for loop.

But you really wouldn't code this as


<code>
class ValueColumn(AbstractColumn):
    def __init__(SELF, NAME, HEADER, DOMAIN_NAMES):
        if type(DOMAIN_NAMES) != tuple:
            raise ValueError('a tuple of domain names must be given')
        for name in DOMAIN_NAMES:
            # Argh, at this point 'name' is readonly, but cannot express that.
            if type(name) != str:
                raise ValueError('a tuple of domain names must be given')
        SELF.domain_names = domain_names
        super(ValueColumn, SELF).__init__(NAME, HEADER)
</code>


Or, at least I wouldn't do that.

It's ugly.

And you have to /change the names/ as the readonly-ness changes.


Cheers,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to