Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 1
    You're accessing those variables using self. self is a direct instance of Child not Parent, hence it can access those stuff. That's how attribute lookup works. Commented Jul 30, 2015 at 20:48
  • is self in Parent class you are referring to? Commented Jul 30, 2015 at 20:50
  • 1
    @Pippi self is the same instance in both places - it is an instance (an object produced from a class) of whatever class you used to instantiate it (Child in this case). From the point of Child, it's an instance of itself. From the point of Parent, it's an instance of one of its subclasses. Commented Jul 30, 2015 at 20:53
  • 1
    @Pippi think of it this way: self always means "an instance of myself or my super- or subclasses" - so yes, methods on the parent class do have access to instance members (attributes or methods) of its subclasses. I recommend Raymond Hettinger's talk Python's Class Development Toolkit on this topic. Commented Jul 30, 2015 at 21:24
  • 1
    (He addresses this specific case at 34m38s) Commented Jul 30, 2015 at 21:31