why the super class can access the subclass's attribute

Y

yousay

I have sees aprogram like this ,i confusing why super class can access
the subclass's attribute
,this is the program,thanks in advance:
class MyThread(threading.Thread):
def join(self):
super(MyThread,self).join()
return self.result

class Worker(MyThread):
import random
import pdb
pdb.set_trace()
def run(self):
total = 0
for i in range(random.randrange(10,100)):
total +=i
self.result = total
 
C

Chris Rebert

I have sees aprogram like this ,i confusing why super class can access
the subclass's attribute

To make this easy to understand, I'm going to ***drastically*** oversimplify.
With that disclaimer out of the way...

When you access an instance attribute, as in self.result, Python
actually returns self.__dict__["result"]; that is, it does a
dictionary lookup using the attribute name as a string as the key;
instance attribute assignment works analogously. Every object has a
special dictionary associated with it that is used to store its
instance attributes; this dictionary can itself be accessed through
the __dict__ attribute. (And if you're about to ask how the __dict__
attribute gets looked up, well, it's magicâ„¢!)

This "instance attribute access is just dictionary manipulation"
principle is universal, even in subclasses, superclasses, and
unrelated objects. Thus, in both Worker and its superclass MyThread,
accessing the `result` instance attribute manipulates the very same
dictionary (self.__dict__) and thus works exactly the same in both
cases.

So, essentially, `result` is accessible to MyThread because there's
nothing to prevent it from being accessible there.

Other related principles that may aid in your understanding (these are
not oversimplified):
- Attribute lookups all happen dynamically at runtime
- Python has no language-enforced notion of `protected` or `private`
attributes; everything's public
- Python is dynamically typed and thus does no compile-time typechecking

Cheers,
Chris
 
J

Jean-Michel Pichavant

yousay said:
I have sees aprogram like this ,i confusing why super class can access
the subclass's attribute
,this is the program,thanks in advance:
class MyThread(threading.Thread):
def join(self):
super(MyThread,self).join()
return self.result

class Worker(MyThread):
import random
import pdb
pdb.set_trace()
def run(self):
total = 0
for i in range(random.randrange(10,100)):
total +=i
self.result = total
I don't thing I got your problem.
But in case you are talking about 'result', you could probably say that
"the subclass Worker can access to the super class MyThread 'result'
attribute".

Also keep in mind that there is no private attributes in python, and
that any object can access any other object attribute, no matter its
class. Of course, you dont want to do that.

JM
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top