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

I just started using object oriented programming in Python. I wander if this is normal, if I create a class method that uses objects from another class. In other words, when I call the method of the first class, I pass the object from the second class as one of the arguments. And then the considered methods (of the first class) can manipulate an object from the second class (to get its attributes or use its methods). Is this allowed in Python? Isn't that considered a bad programming style?

Well, if I create objects from the second class using the first class method. In other words, if I call a method from the first class, it creates objects for the second class.

Thanks in advance for any help.

+4
source share
5 answers

If you are talking about passing an instance of one object to the method of another, then yes, of course, it is allowed! And he considered great practice.

If you want to know more about good object-oriented coding, can I offer some suggested indications:

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, it outlines a series of design patterns that seem to be displayed again and again in object-oriented code. This is a good place for ideas on how to handle certain problems in a good, object-oriented manner.

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 to learn what NOT to do when writing object-oriented code, and how to fix it to make it better when you come across it. It offers a list of code smells that involve poor object-oriented code and a refactoring reference section that provides instructions on how to fix these smells and make them more object oriented.

+7
source

What you are talking about is beautiful. In fact, most data types (string, int, boolean, etc.) in Python are objects, so almost every method works as you described.

+1
source

The answer is that it os MORE than OK, this is actually the whole point.

Which does not mean "OK" when objects begin to mess with the internal elements of each other. You can prevent this from happening by accident by calling things that should be internal with leading underscores (or two, which makes it internal also for subclasses). This works as a small marker for other programmers that you should not use, and that this is not an official API and may change.

+1
source

I do not see a problem with this, it happens all the time. Do you have a specific problem that you are trying to solve, or just ask a general question without context?

0
source

The Law of Demeter is a general guide on which methods and objects with which you can interact in good faith.

This is a guide. You can make code that works that doesn't follow LoD, but it is a good guide and helps you create “structural shy systems” - something you will appreciate later when you try to make big changes.

http://en.wikipedia.org/wiki/Law_of_Demeter

I recommend reading the good OO methods and principles when you are not coding. Maybe a few articles or a chapter of a book every night or every other day. Try SOLID principles. You can find them here:

http://agileinaflash.blogspot.com/2009/03/solid.html

0
source

All Articles