pyside: mutiple modules or multiple classes inside one module?

40 views
Skip to first unread message

Rudi Hammad

unread,
Oct 14, 2017, 5:23:33 PM10/14/17
to Python Programming for Autodesk Maya
Hello,

I am writing a tool in maya with pyside and I was wondering if I should do multiple classes inside a module. After reading different opinions , I didn´t get a clear answer.
To simply my question, imagine that you have a UI with 5 buttons that, when clicked, open different sub-UIs more or less complex..

For instance, one of the buttons opens a simple color picker ui, antoher one opens a dialog with a table widgets, another one opens a more complex UI with its own layout and widgets etc...etc...

Should I create 5 classes inside the same module for this secondary UIs, only 1 class doing all (which will make it less clean because it would have to many methods)
or should I create five modules. The color picker ui is very simple and can be done in less than 20 lines, but other uis could be around 200 lines.
So If I am consistent and decide to write modules, I will have one module with 20 lines, and another with 200.

Thanks,
R

Justin Israel

unread,
Oct 14, 2017, 5:48:06 PM10/14/17
to python_in...@googlegroups.com
My opinion is that if the other widgets are anything more than trivial dumb widgets (a dialog with some initial settings), that it is beneficial to separate them into individual classes. This will help keep your separation of concerns clear and help keep the code maintainable. If your color picker is generic, then making it a discreet class would mean you can reuse it in other code.

 

Thanks,
R

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/76fd4881-de81-462c-955e-a23f75c3eed2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rudi Hammad

unread,
Oct 14, 2017, 7:20:21 PM10/14/17
to Python Programming for Autodesk Maya
Ok. They are related only to 1main UI. So I will create 5 classes inside the same module. My doubt was if I had to do 5 modules.
So I guess that if they were usable in other different UIs it would make sinc eto do different modules, but If they are all relate only to 1 UI, multiple classes if the better choice,


El sábado, 14 de octubre de 2017, 23:48:06 (UTC+2), Justin Israel escribió:
On Sun, Oct 15, 2017 at 10:23 AM Rudi Hammad <rudih...@gmail.com> wrote:
Hello,

I am writing a tool in maya with pyside and I was wondering if I should do multiple classes inside a module. After reading different opinions , I didn´t get a clear answer.
To simply my question, imagine that you have a UI with 5 buttons that, when clicked, open different sub-UIs more or less complex..

For instance, one of the buttons opens a simple color picker ui, antoher one opens a dialog with a table widgets, another one opens a more complex UI with its own layout and widgets etc...etc...

Should I create 5 classes inside the same module for this secondary UIs, only 1 class doing all (which will make it less clean because it would have to many methods)
or should I create five modules. The color picker ui is very simple and can be done in less than 20 lines, but other uis could be around 200 lines.
So If I am consistent and decide to write modules, I will have one module with 20 lines, and another with 200.

My opinion is that if the other widgets are anything more than trivial dumb widgets (a dialog with some initial settings), that it is beneficial to separate them into individual classes. This will help keep your separation of concerns clear and help keep the code maintainable. If your color picker is generic, then making it a discreet class would mean you can reuse it in other code.

 

Thanks,
R

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Oct 14, 2017, 7:46:49 PM10/14/17
to python_in...@googlegroups.com


On Sun, Oct 15, 2017, 12:20 PM Rudi Hammad <rudih...@gmail.com> wrote:
Ok. They are related only to 1main UI. So I will create 5 classes inside the same module. My doubt was if I had to do 5 modules.
So I guess that if they were usable in other different UIs it would make sinc eto do different modules, but If they are all relate only to 1 UI, multiple classes if the better choice,

Oh, different modules just depends if they grow larger and serve completely different purposes. Even if you wanted to reuse them you could keep them all in one module. But if say, one of them had a dependency on another external library, you could separate them so that other apps can still use the ones that don't have dependencies 



El sábado, 14 de octubre de 2017, 23:48:06 (UTC+2), Justin Israel escribió:
On Sun, Oct 15, 2017 at 10:23 AM Rudi Hammad <rudih...@gmail.com> wrote:
Hello,

I am writing a tool in maya with pyside and I was wondering if I should do multiple classes inside a module. After reading different opinions , I didn´t get a clear answer.
To simply my question, imagine that you have a UI with 5 buttons that, when clicked, open different sub-UIs more or less complex..

For instance, one of the buttons opens a simple color picker ui, antoher one opens a dialog with a table widgets, another one opens a more complex UI with its own layout and widgets etc...etc...

Should I create 5 classes inside the same module for this secondary UIs, only 1 class doing all (which will make it less clean because it would have to many methods)
or should I create five modules. The color picker ui is very simple and can be done in less than 20 lines, but other uis could be around 200 lines.
So If I am consistent and decide to write modules, I will have one module with 20 lines, and another with 200.

My opinion is that if the other widgets are anything more than trivial dumb widgets (a dialog with some initial settings), that it is beneficial to separate them into individual classes. This will help keep your separation of concerns clear and help keep the code maintainable. If your color picker is generic, then making it a discreet class would mean you can reuse it in other code.

 

Thanks,
R

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/eb945a4b-210f-4fa7-aa94-f3ebf43b7151%40googlegroups.com.

Phil Sloggett

unread,
Oct 14, 2017, 9:03:23 PM10/14/17
to python_in...@googlegroups.com
My rule of thumb: If in doubt, use separate modules per class, it’s a much nicer place to be as your code grows, or as time goes by and you wonder “where you left that class”. If you KNOW your class is depended on exclusively by other things inside a single module, then maybe they can share the module.

My opinion is that there isn’t much reason not to do a module per class, which goes hand in hand with my opinion that “many smaller modules” > “fewer larger modules”.

Justin Israel

unread,
Oct 14, 2017, 10:25:47 PM10/14/17
to python_in...@googlegroups.com


On Sun, Oct 15, 2017, 2:03 PM Phil Sloggett <philsl...@gmail.com> wrote:
My rule of thumb: If in doubt, use separate modules per class, it’s a much nicer place to be as your code grows, or as time goes by and you wonder “where you left that class”. If you KNOW your class is depended on exclusively by other things inside a single module, then maybe they can share the module.

My opinion is that there isn’t much reason not to do a module per class, which goes hand in hand with my opinion that “many smaller modules” > “fewer larger modules”.

I know these are just opinions and workflow preferences, so it's all good. But I feel like a class per module feels very Java. I usually like to start with one module and then refactor into more than one as the complexity increases.


Joe Weidenbach

unread,
Oct 15, 2017, 12:52:26 AM10/15/17
to python_in...@googlegroups.com
I come from a C# background, so I _get_ the whole module per class structure.  The thing to remember is that python is not the same thing by a long shot.  C#/Java automatically link everything together in a folder, so it all becomes a single module, even with individual files.  .  You want to minimise dependencies between modules as much as possible, and using the 1 class per module structure makes that a *lot* harder.  

Especially as you get more complex in your code structure, it's very easy to get yourself into a situation which has circular dependencies if you're doing that.  The current structure I tend to use for UI's is a single module of widgets, potentially moved out to a different package if/when reuse is needed, once I'm sure I won't have weird circular dependencies creeping in.  I generally start at the specific level in a given library, and then (as needed) bubble the individual classes up to more and more general on an _as-needed_ bases.

The real key is not to try to overarchitect things.  Start with a single module, then break pieces out as it starts to make sense to do so (for separation of concerns, reuse, cutting dependencies etc).  Otherwise you'll tend to paint yourself into corners you might not intend to.

Reply all
Reply to author
Forward
0 new messages