Using "public" vars or attributes in class calls, functional approach

Posted by marw on Programmers See other posts from Programmers or by marw
Published on 2011-06-24T21:33:26Z Indexed on 2011/06/25 0:32 UTC
Read the original article Hit count: 198

I was always wondering about two things I tend to do in my little projects. Sometimes I will have this design:

class FooClass
    ...
    self.foo = "it's a bar"
    self._do_some_stuff(self)

    def _do_some_stuff(self):
        print(self.foo)

And sometimes this one:

class FooClass2
    ...
    self.do_some_stuff(foo="it's a bar")

    def do_some_stuff(self, foo):
        print(foo) 

Although I roughly understand the differences between functional and class approaches, I struggle with the design.

For example, in FooClass the self.foo is always accessible as an attribute. If there are numerous calls to it, is that faster than making foo a local variable that is passed from method to method (like in FooClass2)? What happens in memory in both cases?

If FooClass2 is preferred (ie. I don't need to access foo) and other attributes inside do not change their states (the class is executed once only and returns the result), should the code then be written as a series of functions in a module?

© Programmers or respective owner

Related posts about python

Related posts about functional-programming