Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So there is this c++ code I am migrating to python and I am stuck at one point where I need to write a structure in python, I have handled that by using class. But the difficulty here is the structures has pointers as members and I don't know how to proceed with this
typedef struct {
string_t filename;
string_t version;
node_list_t *node_list;
valtable_list_t *valtable_list;
message_list_t *message_list;
envvar_list_t *envvar_list;
attribute_rel_list_t *attribute_rel_list;
attribute_definition_list_t *attribute_definition_list;
signal_group_list_t *signal_group_list;
network_t *network;
} dbc_t;
In the above code all the data types are type definitions and I am stuck with the pointers on the RHS.
Can someone help me in handling this ?
Thanks! :)
Edit: Retracting my statements as that caused confusion.
All you need to do is assign the variable directly to the member as variables act as reference in case of objects.
You can read more about how python is pass by value for all variables, but how that means its pass by reference for objects here.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 months ago.
Improve this question
If we talk about resources, memory, legibility what would be better between:
Create all the instances at the beginning of a method? (left side of the screenshot)
Create the instances as we need to use them? (right side of the screenshot)
Do you have any documentation that says which is better? I searched in the Python documentation but found nothing.
Well in python creating instances is creating instances, regardless of where you do such, the same amount of instances will be created taking the same amount of storage space.
If you're looking into memory conservation I'd recommend using the del operator when instances are no longer needed.
Other than that this is totally just up to personal preference, and how you'd like to format your code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to access a module I have written in Python from C? Is this at all possible?
I have tried:
Module = PyImport_ImportModule("<modulename>");
But it doesn't seem to work
please try to add more information of what are you trying to do.
Anyway, I know it is possible. It isn't easy though..
Have you tried looking for any tutorials? I think this was already asked:
Call a Python function from within a C program
https://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I
https://www.linuxjournal.com/article/8497
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am starting to learn about the hash table data structure in C, and I've noticed (if I understand the concept correctly) that hash tables are awfully similar to python dictionaries. If I am incorrect and they are two completely different things, could someone explain to me what a hash table is without getting too technical? Thanks.
There is not really any difference between them. That's why python's dicts don't support duplicates. That is also why python has the function hash which python's dictionaries use by default.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So i have a program where the user enters in a few details e.g name, place etc. However, when i collect these details, they are all assigned to the variable name that i gave to the input collector (e.g input =). But i need to process these variables into a specific order (its a bibliography generator fyi). However i need to assign each of the instances of inputs into different variables without having basically the same code over and over again, but i couldn't think of a way to do that.
Help would be much appreciated.
Here is how I solve that problem in the past:
1) Store the data in an ordered dictionary.
2) Store the data in an object and use a function that returns the data in the order specified.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I hope there is a 'right' answer to this:
When using ConfigParser to manage default values in a .cfg or .ini file, should I copy everything I need to program variables and copy them back out on exit, or should I use the config.get(section, option) directly in my program as needed?
This is really a matter of opinion, but my advice is to have the values out of the config relatively quickly. The code that deals with data input and the layer that deals with the actual processing should be modular enough that you can change your data source by just feeding in data from a different source. (Coupling and Cohesion)
You'll have to use your own judgement to make the call as to where to draw the line, but as a guide: if you're setting the config as a global variable and reading from there or constantly throwing it around as an argument, you're doing it wrong.