0

Here is an excerpt of my class:

Person:
   __init__(self, first_name):
      self.first_name = first_name

Should I create a function that allows me to get or change the value of the instance variable like this:

def set_first_name(self, first_name):
    self.first_name = first_name

Or is it okay to just use something like person1.first_name = ...?

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52

1 Answers1

0

Do whatever you want (it's "ok"), a similar question was asked recently too in which I said that creating functions for such purposes will have a minimal overhead so unless you're using a function to externally set properties thousands of times, it won't matter.

It's obvious that without optimizations indexing and setting properties directly should be quicker (just quicker considering a nearly infinitesimal difference). Readability should be your biggest concern if you're considering dedicating a function to altering properties anyway.

un-index
  • 578
  • 3
  • 21