Python: Using derived class attributes in base class

mittal

I was reading through some code when I came across this particular code where the base class method is printing some attributes which are from derived class, the object which is calling the method is from derived.

class A(object):
  def printFreak(self):
    print self.derived_attrib

class B(A):
  def __init__(self, num):
    self.derived_attrib = num

my_obj = B(10)
my_obj.printFreak()

Since I had not seen such behaviour before(like in C++), I am unable to understand this.

Can anyone help me understand this, how this works ? Can this be related to some concept of C++ ?

khelwood

In Python, attributes are resolved at run-time, so it simply looks for an attribute called derived_attrib in the object referred to by self, and finds that there is one.

It would work in C++ as long as derived_attrib was declared as field of A and then assigned in B, because then the compiler would be able to figure out what self.derived_attrib meant in A's method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Derived Class In a Base Method

From Dev

Python: Hide member of base class in derived class

From Dev

Python access derived class attributes in parent constructor

From Dev

class derived from class template using base:base in body

From Dev

Convert a base class to a derived class

From Dev

composition of a derived class in a base class

From Dev

Base class to Derived class in Iteration

From Dev

composition of a derived class in a base class

From Dev

base pointer to derived class

From Dev

Cast derived class to base

From Dev

Constructor in base and derived class

From Dev

Derived class with extra private attributes

From Dev

Inheritance : using base class or derived class to do stuff

From Dev

Use typedef/using from templated base class in derived class

From Dev

Call derived class method when using base class in foreach loop

From Dev

Conflict in return type from base class with derived class using auto

From Dev

Call templated function with derived class arguments using base class pointers

From Dev

How to return a derived class using only code in the base class?

From Dev

Base class static method using constants of derived class

From Dev

C++ using static array in base class, declaring in derived class

From Dev

Creating derived class instance using base class instance

From Dev

Base class static method using constants of derived class

From Dev

Python inheritance: convert from Base class to Derived class

From Dev

Using/storing derived member in derived class with base class that stores base member

From Dev

Python - Updating Child Attributes with a Base Class Method

From Dev

Python - Updating Child Attributes with a Base Class Method

From Dev

How to get access to the attributes of an derived class from the base class in C++?

From Dev

Accessing class attributes in base (parent) class method with possible overloading by derived (children) classes

From Dev

Is it possible to use the base class constructor to initialize values to base class private members using derived class?

Related Related

  1. 1

    Using Derived Class In a Base Method

  2. 2

    Python: Hide member of base class in derived class

  3. 3

    Python access derived class attributes in parent constructor

  4. 4

    class derived from class template using base:base in body

  5. 5

    Convert a base class to a derived class

  6. 6

    composition of a derived class in a base class

  7. 7

    Base class to Derived class in Iteration

  8. 8

    composition of a derived class in a base class

  9. 9

    base pointer to derived class

  10. 10

    Cast derived class to base

  11. 11

    Constructor in base and derived class

  12. 12

    Derived class with extra private attributes

  13. 13

    Inheritance : using base class or derived class to do stuff

  14. 14

    Use typedef/using from templated base class in derived class

  15. 15

    Call derived class method when using base class in foreach loop

  16. 16

    Conflict in return type from base class with derived class using auto

  17. 17

    Call templated function with derived class arguments using base class pointers

  18. 18

    How to return a derived class using only code in the base class?

  19. 19

    Base class static method using constants of derived class

  20. 20

    C++ using static array in base class, declaring in derived class

  21. 21

    Creating derived class instance using base class instance

  22. 22

    Base class static method using constants of derived class

  23. 23

    Python inheritance: convert from Base class to Derived class

  24. 24

    Using/storing derived member in derived class with base class that stores base member

  25. 25

    Python - Updating Child Attributes with a Base Class Method

  26. 26

    Python - Updating Child Attributes with a Base Class Method

  27. 27

    How to get access to the attributes of an derived class from the base class in C++?

  28. 28

    Accessing class attributes in base (parent) class method with possible overloading by derived (children) classes

  29. 29

    Is it possible to use the base class constructor to initialize values to base class private members using derived class?

HotTag

Archive