Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK if objects from different classes interact with each other?

Tags:

python

oop

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.

like image 465
Verrtex Avatar asked Dec 29 '22 14:12

Verrtex


1 Answers

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.

like image 135
Daniel Bingham Avatar answered Jan 27 '23 09:01

Daniel Bingham