Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a C++ class in python class as superclass [duplicate]

Tags:

c++

python

Possible Duplicate:
How can I use C++ class in Python?

I am designing a software in python but I am experiencing some memory problems while implementing it with python so I am planning to extend my software with C++. So I was thinking can I use C++ defined class and use it in python as Superclass and override some of its methods?

like image 224
Robins Gupta Avatar asked Sep 28 '12 10:09

Robins Gupta


People also ask

What is super () __ init __ in Python?

The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super. The super() function has two major use cases: To avoid the usage of the super (parent) class explicitly.

What is the effect of calling super () __ Init__?

__init__() of the superclass ( Square ) will be called automatically. super() returns a delegate object to a parent class, so you call the method you want directly on it: super(). area() . Not only does this save us from having to rewrite the area calculations, but it also allows us to change the internal .

What is __ new __ in Python?

__new__ is static class method, while __init__ is instance method. __new__ has to create the instance first, so __init__ can initialize it. Note that __init__ takes self as parameter. Until you create instance there is no self . Now, I gather, that you're trying to implement singleton pattern in Python.

Can you inherit from 2 classes Python?

In Python a class can inherit from more than one class. If a class inherits, it has the methods and variables from the parent classes. In essence, it's called multiple inheritance because a class can inherit from multiple classes. This is a concept from object orientated programming.


1 Answers

The answer is yes.

You can inherit and extend the class using boost.python. Please take a look how to do that.

However, to override the methods of base C++ class the way is bit more complex, please take a look at this article on the subject.

Also, I would have considered to think how to avoid using inheritance and use wrappers instead for your C++ class, if that is possible.

like image 88
pershyn Avatar answered Sep 17 '22 13:09

pershyn