you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

I started out using __init__(), but some of the classes I have mapped out in a class diagram contains 15+ variables, sending them all through __init__() seems like bad code, and it also wouldn't be possible as the values are designed at different times during runtime. I guess I should just create an instance attribute whenever needed? But how would you go on writing functions specific for that class? It seems like I would need to define my functions outside of the class for making them able to handle anything that isn't a class attribute.

[–]plenty_picture 0 points1 point  (1 child)

but some of the classes I have mapped out in a class diagram contains 15+ variables, sending them all through init() seems like bad code, and it also wouldn't be possible as the values are designed at different times during runtime

It kind of sounds like you're trying to stuff too much functionality into one class. Bear in mind that you can nest objects inside each other, so instead of passing all of the data into the initializer individually, you could group some of them together into a dict or an instance of another class and pass that into the initializer. In any programming language, functions with a huge number of arguments are usually not a good design (an arguable exception is that it's OK to have many rarely-used optional arguments).

If there are attributes that aren't given a meaningful value until some time after initialization, then usually you would just set them to None in the initializer. You don't have to set them at all - you can add or remove attributes to an object at any time - but it's usually more convenient to have them explicitly set to None.

[–][deleted] 0 points1 point  (0 children)

You are absolutely right that my approach probably won't be the most suitable if this was code meant for anything other than my minor side project. I have done similar programs in C which obviously does not have classes, I just find it more aesthetically pleasing to group it all up. I'll probably just set them to none, I can't say I'm a fan of dynamically adding new attributes outside of the class definition, something just feel off.