tags:

views:

185

answers:

5

Hi, 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.

+6  A: 

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.

Daniel Bingham
Great. Thank you. By the way, Is it OK if objects from different classes interact with each other?
Verrtex
Again, yes, it is, but it depends how they interact with each other and why. Take a look at the two books I suggested. They cover all of these questions and more in great detail. They are required reading pretty much for anyone doing object oriented design (at least, the first one is).
Daniel Bingham
I have only glanced at the Design Patterns book, but it looked like pretty heavy reading. You might want to look instead at the book Head First Design Patterns, which looks a bit more lively. (Then go ahead and read the original, if you still want to!)
steveha
And I'll put in a plug for the book Learning Python from O'Reilly; that covers the whole Python language very well, and will give you a good start on Python object-oriented idioms.
steveha
A: 

I see no problem with that, it happens all the time. Do you have a specific problem you're trying to solve or just asking a general question without a context?

vehomzzz
At the moment it is just a general question. Am am thinking about the general things (how to organize my code). By the way, can I instantiate objects of the second class from a method of the first class?
Verrtex
you can but it is NOT a good idea. Why don't you have a factory do create objects (unless you mean that second classes to be designated as a factory, than it is fine).
vehomzzz
Yes. In fact there is a design pattern for a class who's sole purpose is to do just that, it's called a Factory class.
Daniel Bingham
+1  A: 

What you're talking about is fine. In fact most data types (string, int, boolean, etc.) in Python are objects, so pretty much every method works in the way you described.

GSto
+1  A: 

The answer is that it os MORE than OK, it's in fact the whole point.

What is not "OK" is when objects start fiddling with the internals of each other. You can prevent this from happening accidentally, by calling things that are meant to be internal with a leading underscore (or two, which makes it internal also for subclasses). This works as a little marker for other programmers that you aren't supposed to use it, and that it's not official API and can change.

Lennart Regebro
A: 

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

It is guidance. You can make code that works that doesn't follow the LoD, but it is a good guide and helps you build "structure shy systems" -- something you will appreciate later when you try to make big changes.

http://en.wikipedia.org/wiki/Law%5Fof%5FDemeter

I recommend reading up on good OO practices and principles when you're not coding. Maybe a few papers or a chapter of a book each evening or every other day. Try the SOLID principles. You can find a quick reference to them here:

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

tottinge