why the super class can access the subclass's attribute

Discussion in 'Python' started by yousay, Jan 21, 2010.

  1. yousay

    yousay Guest

    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
     
    yousay, Jan 21, 2010
    #1
    1. Advertising

  2. yousay

    Chris Rebert Guest

    On Wed, Jan 20, 2010 at 8:56 PM, yousay <> wrote:
    > 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
    --
    Simplifications include overlooking __slots__, metaclasses,
    __getattribute__, and instance.class_attribute, among others.
    http://blog.rebertia.com

    > ,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
     
    Chris Rebert, Jan 21, 2010
    #2
    1. Advertising

  3. yousay wrote:
    > 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
     
    Jean-Michel Pichavant, Jan 21, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,792
    Darryl Pierce
    Feb 24, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,000
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  4. fi3nd
    Replies:
    0
    Views:
    384
    fi3nd
    Apr 8, 2010
  5. Trans
    Replies:
    8
    Views:
    323
    Robert Klemme
    Oct 23, 2008
Loading...

Share This Page