N
[email protected] said:[test 1]... i = 1
...... i = 1[test2]a = A()
A.i 1
a.i 1
A.i = 2
A.i 2
a.i 2class A:
...
Is there somthing wrong????
I don't think so, the name binding stuff again. Good to know though.[test 1]... i = 1
...... i = 1[test2]a = A()
A.i 1
a.i 1
A.i = 2
A.i 2
a.i 2class A:
...
Is there somthing wrong????
Is there somthing wrong????
I believe he knows about inheritance, but not about the behaviour ofSteven said:Kids today, don't they learn about inheritence?
Python's object model is that instances inherit both
methods and attributes from the class (and
superclasses). Methods are just a special case of
attributes: the method is a callable attribute.
When you reference an attribute, Python first checks
the instance by looking up instance.__dict__, and if
that fails, it looks up instance.__class__.__dict__.
(This is a simplification, e.g. it isn't exactly true
for objects with slots.)
For attribute lookup (that is, the attribute reference
is on the right hand side of an assignment), the lookup
may fail and so the class attribute may be retrieved.
This is by design.
For attribute assignment (that is, the attribute
reference is on the left hand side of an assignment),
the assignment will never fail.
(Again, ignoring slots and any other special cases I
have't thought of.)
[test 1]
... i = 1
...
1a = A()
A.i
1
2A.i = 2
A.i
2
[test2]
class A:
... i = 1
...
1
1
1
2
Is there somthing wrong????
[email protected] said:I believe he knows about inheritance, but not about the behaviour of
the assignment. In many other OO languages, I believe you cannot have
the same name for both instance variable and class variable. javascript
has similar behaviour.
[snip]Kids today, don't they learn about inheritence?![]()
I believe he knows about inheritance,
but not about the behaviour of
the assignment.
In many other OO languages, I believe you cannot have
the same name for both instance variable and class variable. javascript
has similar behaviour.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.