1

Consider the following example:

class Foo(object):
    def bar(self):
        return "bar"

class Bar1(object):
    def __init__(self):
        self.foo = Foo()
    def bar(self):
        return self.foo.bar()

class Bar2(object):
    def __init__(self):
        self.bar = Foo().bar

Bar1().bar() == Bar2().bar()

Which of Bar1 and Bar2 should one prefer? What are the pro/cons of the alternatives?

What I'm trying to ask is that there is an instance which has only one method, which is used in another instance. Should I make it clear that it is indeed a "method" (Bar1) or should I save one function call and some complexity (Bar2)?

2
  • 1
    Option 3: Bar3().foo.bar() to make it clear that there is a different object involved. If Foo’s bar is completely unrelated to the Bar type, it makes no sense to expose it as a Bar method instead. Commented Sep 17, 2013 at 10:55
  • I echo @poke's point. Where does the method naturally fit? Is it something that acts on Bar, or is it something that acts on some attribute of Bar? Put it where it feels correct. In common with the answers below, Bar2 seems odd to me. You're pretending a method on Foo is a method on Bar, but it's always bound to a particular instance of Foo, so you're going to very much break expectations. Commented Sep 17, 2013 at 13:18

2 Answers 2

2

Bar2 is clearly a hack (probably to reduce source length and maybe to reduce call overhead). I would not do this.

Reasons:

Bar2().bar is a method of Foo (which is surprising and thus no good style). You can check this yourself by having a look at Bar2().bar.im_self.

② Overriding the method of Bar2 is not possible for subclasses. You disable a valuable feature of classes this way.

③ Changing the Bar's foo later will only have the expected effect in Bar1 while in Bar2 the pointer to the method of the original foo will still be in use.

Sign up to request clarification or add additional context in comments.

Comments

0

Should I make it clear that it is indeed a "method" (Bar1)

You should try not to expose the implementation of Bar to the user. Why would he care if it calls a method of some internal class? What's important is how the external API call behaves.

If you make it a method, it will imply that:

  • It changes the internal state of the object, or
  • It requires re-calculation on each call (just a silly example, if it represents the amount of time since the creation of the object), or
  • It takes a (relatively) significant amount of time to execute and cannot be cached.

Neither seems to be applicable in your case (of course, you have more information, since you actually know what bar() does, and how its behavior may change in the future).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.