data attributes override method attributes?
Peter Otten
__peter__ at web.de
Tue Sep 25 10:54:12 EDT 2012
alex23 wrote:
> On Sep 26, 12:08 am, Peter Otten <__pete... at web.de> wrote:
>> Jayden wrote:
>> > In the Python Tutorial, Section 9.4, it is said that
>>
>> > "Data attributes override method attributes with the same name."
>>
>> The tutorial is wrong here. That should be
>>
>> "Instance attributes override class attributes with the same name."
>>
>> As methods are usually defined in the class and data attributes are
>> usually set in the instance it will look like data override method
>> attributes.
>
> But you can assign attributes on the class, which has the same impact,
> so the tutorial is correct.
>
>> No, you're right. Please file a bug report athttp://bugs.python.org
>
> Didn't you just demonstrate the behaviour you're now saying is a bug?
>
To me
"Data attributes override method attributes with the same name"
implies that data attributes take precedence over method attributes, not
that they replace them only when there is an assignment of data after the
method definition.
With your interpretation (if I understand you correctly)
"Method attributes override data attributes with the same name"
is equally correct, and therefore I think it is misleading to focus on the
type of the attributes at all.
I would even consider replacing the whole paragraph
"""
Data attributes override method attributes with the same name; to avoid
accidental name conflicts, which may cause hard-to-find bugs in large
programs, it is wise to use some kind of convention that minimizes the
chance of conflicts. Possible conventions include capitalizing method names,
prefixing data attribute names with a small unique string (perhaps just an
underscore), or using verbs for methods and nouns for data attributes.
"""
http://docs.python.org/dev/py3k/tutorial/classes.html
with something like
"Data attributes and method attributes share the same namespace. To avoid
name conflicts consider using verbs for methods and nouns for data
attributes"
More information about the Python-list
mailing list