First script with Python OOP - any tips?

35 views
Skip to first unread message

Korinkite

unread,
Dec 2, 2014, 12:21:46 AM12/2/14
to python_in...@googlegroups.com
Hiya! Long time lurker, first time commenter. I'm learned more from this group just browsing through people's inqueries and questions than I have in an entire class in programming python in Maya. I really hope to contribute here soon!

The next script I'm about to write will require using python classes and I'm trying to learn 
as much about python classes as I can. To that end, I'll definitely give the sticky page another thorough look. In the meantime though, I'd like to ask some "process" questions directed towards anyone who has experience coding with class/object-oriented in python. How do you typically go about "designing" your classes? Do you have a tried-and-true method to plan and develop your scripts? Does Maya's integration with python change the way you have to design classes?

Last thing:
Like class structures, I haven't used PyQt in a formal project before in Maya but I'm considering just "learning as I go" like I did with PyMEL because it integrates with classes well. Would you advise for/against using learning PyQt while also figuring out your first class-based python script?

I'm fortunate in that my script is a personal project but I don't want to go in blind either. I'd appreciate any insight! Thank you! (I posted on cgtalk as well, I hope that's allowed)

Justin Israel

unread,
Dec 2, 2014, 1:40:01 AM12/2/14
to python_in...@googlegroups.com
Hi,

It's a good idea to first understand when you want a class and when you don't. If you a common set of operations that need to save and share state between them, and you have at least 2 of these operations, then you might need a class. On the other hand, if you only want to save state, or you only have a single operation, then you probably want a dict. 

When you decide to write a class, you want it to have a single responsibility. Then you figure out first and foremost what your public interface should be. What are the methods you want other people to call if they create an instance of your class? Ideally you would expose the least amount of public methods possible, which will make your class more encapsulated and easier to maintain. Same goes for public vs private attributes. 

Another good suggestion is not to go crazy with classes and inheritance. That is, don't try and create a big type hierarchy upfront based on what you think you will need. Best to just design exactly what you need for a concrete use, and then to refactor later when you need to introduce abstractions. 

Specifically on the topic of Qt... I said something similar in my Python/Qt training videos, that Qt is really heavy on the concept of classes. Pretty much everything deals with types and subtypes, so it would be a good idea to get comfortable with classes first. It isn't required though, and you will learn a lot by playing with Qt anyways. It just means your first class structures might be a total mess :-) 
The main goal is to create nice clean interfaces in your classes to hide away the private details and make each piece kind of a black-box component. It will help with reusability.

Justin

 

--
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/66bbb68d-50b2-44ed-b8a5-e686f8e1e189%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages