import numpy as np
class MyClass:
a = int()
b = float()
c = np.array([0 for i in range 10])
def Memberfunction():
print("Hii")
Asked
Active
Viewed 1,842 times
-3

Keyur Potdar
- 7,158
- 6
- 25
- 40

Prateek Sarangi
- 13
- 2
-
For such code how can I define the member function outside the class? Thank you in advance – Prateek Sarangi Jun 10 '18 at 13:23
-
I have no idea what you mean. – Rohi Jun 10 '18 at 13:23
-
I have defined the member function MemberFunction inside the class MyClass, how can I do it outside the class as we do it in C++, like class MyClass{ int MemberFunction(); } int MyClass::MemberFunction(){ cout<<"HII"; } How to write such code in python? – Prateek Sarangi Jun 10 '18 at 13:27
-
Why would you ever want to do that? – Aran-Fey Jun 10 '18 at 13:36
-
Just checking if it is possible or not. – Prateek Sarangi Jun 10 '18 at 13:37
1 Answers
1
class test_class():
def __init__(self, some_func):
self.some_value = 2
setattr(type(self), some_func.__name__, some_func)
def my_requested_func(self):
print(self.some_value)
class_instance = test_class(my_requested_func)
class_instance.my_requested_func()
This should work, but I dont see any reason for you wanting to do this.

Rohi
- 814
- 1
- 9
- 26
-
Yes but defining the Memberfunction() outside the class MyClass – Prateek Sarangi Jun 10 '18 at 13:38
-
-
'test_class' object has no attribute 'some_func', Error shown, please help me out. – Prateek Sarangi Jun 10 '18 at 13:44
-
-