Tuesday, 15 February 2011

python - Method and attribute -



python - Method and attribute -

i wondering, happens if method , attribute has same name in class. programme first search attribute in object , if method in class has same name, programme skip , focus on object?

it depends on 1 assigned first.

if method assigned first, attribute 1 access,

e.g.

class example(object): def __init__(self): self.x = 44 def x(self): homecoming "hi" print example().x

methods "assigned" when class processed, x assigned @ initialization. means x overwrites method.

this of course of study different class level attributes because assigned @ same time method

class example(object): x = 43 def x(self): homecoming "hi" print example().x

shows method overwrites attribute.

python class methods

No comments:

Post a Comment