1

I'm curious if there is a convention in python for using the self variable in the __init__ method of a class.

Consider the simple Person class.

class Person:

    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

        self.fullname = ' '.join([self.firstname, self.lastname])

The __init__ method stores the two inputs firstname and lastname as instance variables and then uses these instance variables to define the fullname instance variable.

It seems like the self variable is unnecessary when defining self.fullname, so Person could alternatively be written as:

class Person:

    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

        self.fullname = ' '.join([firstname, lastname])

Is there any meaningful difference between these two ways of writing the class? I don't see a difference other than the fact that the second option requires less horizontal space to define self.fullname, which might be desirable.

Brian Fitzpatrick
  • 2,443
  • 2
  • 14
  • 14
  • 2
    In your trivial example, no, there is no reason to use `self` when creating `fullname`. The two produce identical results. – g.d.d.c Jul 19 '19 at 22:47
  • there's no difference whatsoever in the result, given the assigments just above. I don't see much advantage to writing it the first way, unless you expect you might later change how you assign to `self.firstname` and `self.lastname`. – Robin Zigmond Jul 19 '19 at 22:48
  • The only difference I see is whether you draw the `fullname` components from the input arguments, or from the previously initialized object attributes. The result is the same. – Prune Jul 19 '19 at 22:48
  • 1
    If you later decide to normalize the arguments, or provide default values, the first code sample would require less effort to change. The difference is pretty minimal however: I wouldn't worry about this sort of thing unless it starts to cause problems. – Patrick Haugh Jul 19 '19 at 22:52
  • Possible duplicate of [What is the purpose of the word 'self', in Python?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self-in-python) – Deep Bhatt Jul 19 '19 at 22:56
  • Since it wouldn't affect the result, there is no difference in your example. It would be infinitesimally faster not to use the "self." prefix, but probably not enough to matter. – martineau Jul 20 '19 at 07:05
  • @deepbhatt this is obviously not a duplicate of the question you've referenced. – Brian Fitzpatrick Jul 21 '19 at 04:53

1 Answers1

1

The second example is a (very slightly) more efficient way to write the same code.

In general the methods of an instance use instance variables to maintain the instance's state. Since each method gets the same object as self, the different methods can communicate over time through their use.

In both examples firstname and lastname are so-called "local variables," whose lifetime is only that of the method in which they are bound. Because they can be looked up more quickly (for reasons we needn't go into) as well as easier to type they are usually preferred for values with a limited lifetime.

holdenweb
  • 33,305
  • 7
  • 57
  • 77