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.