data attributes override method attributes?

alex23 wuwei23 at gmail.com
Tue Sep 25 09:52:26 EDT 2012


On Sep 25, 11:41 pm, Jayden <jayden.s... at gmail.com> wrote:
> Dear All,
>
> In the Python Tutorial, Section 9.4, it is said that
>
> "Data attributes override method attributes with the same name."
>
> But in my testing as follows:
>
> #Begin
> class A:
>         i = 10
>         def i():
>                 print 'i'
>
> A.i
>    <unbound method A.i>
> #End
>
> I think A.i should be the number 10 but it is the method. There must be something I misunderstand. Would you please tell me why?

What the tutorial is referring to is this, I think:

    class A(object):
        def i(self):
            print 'i'

    >>> a = A()
    >>> a.i = 10
    >>> a.i
    10

That is, you can create a data attribute on an object even if a method
attribute of the same name exists.



More information about the Python-list mailing list