I just started to use the object oriented programming in Python. I wander if it is OK if I create a method of a class which use objects from another class. In other words, when I call a method of the first class I give an object from the second class as one of the arguments. And then, the considered methods (of the first class) can manipulate by the object from the second class (to get its attributes or use its methods). Is it allowed in Python? Is it not considered as a bad programming style?
Is it OK if I instantiate objects from the second class withing a method of the first class. In other words, if I call a method from the first class it instantiate objects fro the second class.
Thank you in advance for any help.
If you're talking about passing an instance of one object to the method of a another one, then yes of course it's allowed! And it's considered fine practice.
If you want to know more about good object oriented coding, may I offer some suggested readings:
Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides
Known as the Gang Of Four book, this lays out a number of design patterns that seem to show up in object oriented code time and time again. This is a good place to go for ideas on how to handle certain problems in a good object oriented way.
Another good one:
Refactoring: Improving the Design of Existing Code by Martin Fowler, Kent Beck, John Brant , William Opdyke, Don Roberts
This is a great book for learning what NOT to do when writing object oriented code, and how to fix it to make it better when you do encounter it. It offers a list of code smells that suggest bad object oriented code and a reference section of refactorings that provide instructions on how to fix those smells and make them more object oriented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With