Click here to Skip to main content
15,565,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to solve this problem but still in pycharm, when i'm looking at my function argument
name i see only "*args" and "**kwargs" despite "labels_list" which i set while defining my function in
I can pass argument with keyword "labels_list" or without it, but i can't see this argument name
in function arguments list in PyCharm.

Here is my code:

static PyObject* encode_one_hot(PyObject* self, PyObject* args, PyObject* kwargs) {
    PyArrayObject* labels = NULL;
    PyArrayObject* one_hot;
    npy_intp dims[2];
    map<int, int> classes_map;
    int current_label;
    int labels_size;
    int new_numeration = 0;
    void* ptr;
    static char* kwlist[] = { (char*)"labels_list", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &labels))
        return NULL;
    PyArray_Sort(labels, 0, NPY_QUICKSORT);
    labels_size = PyArray_SIZE(labels);
    for (int i = 0; i < labels_size; i++) {
        ptr = PyArray_GETPTR1(labels, i);
        current_label = PyLong_AsLong(PyArray_GETITEM(labels, ptr));
        if (classes_map.find(current_label) == classes_map.end()) {
            classes_map[current_label] = new_numeration;
    dims[0] = labels_size;
    dims[1] = (int)classes_map.size();
    one_hot = (PyArrayObject*)PyArray_ZEROS(2, dims, NPY_INT, 0);
    for (int i = 0; i < labels_size; i++) {
        current_label = classes_map[PyLong_AsLong(PyArray_GETITEM(labels, PyArray_GETPTR1(labels, i)))];
        ptr = PyArray_GETPTR2(one_hot, i, current_label);
        PyArray_SETITEM(one_hot, ptr, PyLong_FromLong(1));
    return PyArray_Return(one_hot);

I'll be very grateful for help :)

What I have tried:

I've tried to look on google for any answer and read documentation.
Updated 22-Jun-20 4:36am

1 solution

The names of variables outside the function aren't passed to the function in C or C++ - all that is passed is the content.
In your case, what is passed is a pointer - a memory address - and it does not have a name.
Worse, if your calling function stored the data in a local variable, it is quite likely that it never had a name once the app was compiled, as many optimisers would use machine registers instead of storing anything in memory.
Share this answer
Eldorad O. 22-Jun-20 12:09pm    
So how can i name my C function parameters in Python?
I thought about something like this:
from module_with_function_written_in_C import encode_one_hot

def encode_one_hot_python(labels_list):
    return encode_one_hot(labels_list)

But it looks ugly for me :/
OriginalGriff 22-Jun-20 12:19pm    
Just call them the same as in the C code:
self, args, and kwargs
That way, they "tie up" with the names in C code and you can see what they are meant to do / be.
Eldorad O. 22-Jun-20 13:13pm    
Let me show you what I mean, maybe I don't describe my problem precisely ;d


I want to "labels_list" appear in place of arguments names despite "*args, **kwargs".
OriginalGriff 22-Jun-20 14:02pm    
You can't: the C code requires three different parameters, not a single collection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900