Home > Mobile >  proper way to define functions inside class python
proper way to define functions inside class python

Time:11-24

I'm a newbie in python, I have some python code that interfaces with a dll library (in C). My python code work fine as it is now, but I was told to implement some of the initializations into their own functions. For example, what I currently have is:

hello.py

import ctypes

class my_outer_class:

    def __init__(self):

        test = ctypes.WinDLL('C:\\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')

        self.py_function_1 = test.function_1
        self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int

        self.py_function_2 = test.function_2
        self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_2.restype = ctypes.c_int

run_test.py

import hello
import ctypes

myapi = hello.my_outer_class()
            
result = myapi.py_function_1(123,123)
print(result)

result = myapi.py_function_2(123,123)
print(result)

I was wondering if I could change my hello.py to something as:

hello.py

import ctypes

class my_outer_class:

    def __init__(self):

        test = ctypes.WinDLL('C:\\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')

        def func_1(self):
            self.py_function_1 = test.function_1
            self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
            self.py_function_1.restype = ctypes.c_int

        def func_2(self):
            self.py_function_2 = test.function_2
            self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
            self.py_function_2.restype = ctypes.c_int

run_test.py

import hello
import ctypes

myapi = hello.my_outer_class()
            
result = myapi.func_1(123,123)
print(result)

result = myapi.func_2(123,123)
print(result)

when I run the modified version I get the error:

Traceback (most recent call last):
  File "C:\Users\OneDrive\run_test.py", line 6, in <module>
    result = myapi.func_1(123,123)
AttributeError: 'my_outer_class' object has no attribute 'func_1'
>>> 

I appreciate any suggestions, thanks.

modifying hello.py to

import ctypes

class my_outer_class:

    def __init__(self):

        self.test = ctypes.WinDLL('C:\\Users\giova\OneDrive\Escritorio\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')

    def func_1(self, var1, var2):
        self.py_function_1 = self.test.function_1
        self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int

    def func_2(self, var1, var2):
        self.py_function_2 = self.test.function_2
        self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_2.restype = ctypes.c_int

and run_test.py

import hello
import ctypes

myapi = hello.my_outer_class()
            
result = myapi.func_1(123,123)
print(result)

result = myapi.func_2(123,123)
print(result)

At this point I don't get any errors, I get this output

None
None
>>> 

Instead of the expected values of 1 and 0. I was able to obtain these values in the first version of my code. Also does the other two parameters beside "self" need to match what I have under argtype? for example

def func_1(self, ctypes.c_uint8, ctypes.c_uint8):

because I tried this way and it gave me an invalid syntax error.

   def func_1(self, ctypes.c_uint8, ctypes.c_uint8):
                           ^
SyntaxError: invalid syntax 

CodePudding user response:

Essentially, you are confronted with an indent-problem here.

Please re-indent your func x in your class to make them externally accessible. Here is a brief illustration:

class my_outer_class:

    def __init__(self):
        print("initiated")

    def func_1(self):
        print("Hello FUNC_1")

    def func_2(self):
        print("Hello FUNC_2")

myapi = my_outer_class()

myapi.func_1()
myapi.func_2()

For a more in-depth look into classes and object-oriented programming in Python, you could start with the following: https://python-textbok.readthedocs.io/en/1.0/Classes.html

  • Related