[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Design: method in class or general function?

On Thu, Sep 7, 2017 at 8:16 AM, Steve D'Aprano <steve+python at pearwood.info>

> On Thu, 7 Sep 2017 07:20 pm, Leam Hall wrote:
> > OOP newbie on Python 2.6.
> Python 2.6 is ancient, and is missing many nice features. You should
> consider
> using the latest version, 3.6.

I've wrestled with that discussion for a while and Python 3 loses every
time. There's literally no good reason for me to move to Python 3 earlier
than mid-2020's. Please accept the fact that there are hundreds of
thousands of servers, if not millions, running Python 2.x. Whether or not
Python 3 has any neat cool stuff is irrelevant to those of us seeking to
use Python to get today's work done.

> I create instances of Character class with an attribute dict of
> > 'skills'. The 'skills' dict has the name of a skill as the key and an
> > int as a value. The code adds or modifies skills before outputting the
> > Character.
> >
> > Is it better design to have a Character.method that takes a 'skill' key
> > and optional value or to have a general function that takes an instance,
> > a dict, a key, and an optional value?
> I'm afraid your example is too generic for me to give an opinion. Do you
> literally mean a method called "method"? What does it do?

Using this:

Line 19 sets "self.skills" either from the passed in data or from

So Character.skills is a dict with a string key and an int value. I need to
be able to add skills and my first attempt is a function:

Should the "add_skills" function be a method in the character class or be
made a more generic function to add/modify a key/value pair in a dict that
is an attribute of an instance? Other tasks will require the add/modify
functionality but coding that increases complexity. At least for me, anyway.

Sorry about being unclear earlier, coffee was still kicking in and I'm
still a newbie that mixes up terms.