Annoyance: init
functions often immediately begin by copying the inputs into class member variables
class planet_gravity:
def __init__(self, r, m):
# define class variables for each input
self.r = r
self.m = m
# define variables that are functions of the input variables
self.g = 6.6E-11 * self.m/(self.r** 2)
In addition to extra typing for each input in the init
function, this repetition also makes it more difficult to change the name of a variable as there are two versions of it (e.g. r
and self.r
).
Similarly, when a variable in the init function has two versions (self.variable
and variable
), one can freely switch between the two. However, if the code is moved to another class member function, it will only work with the self.variable
version, making it cumbersome to simply move code from the init
function elsewhere as the init
function becomes complex.
There exist packages which can automate this process (e.g. dataclasses
), but they are often focused on data-only classes or are quite sophisticated. A syntax for simplifying this common coding pattern would be helpful.
Proposal: Allow inputs to class member functions to include the self
modifier.
Whenever a variable is inserted into the init
function prepended with self, it is syntatic sugar for the following replacement
def __init__(self, ..., self.my_variable, ...):
pass
Is equivalent to
def __init__(self, ..., my_variable, ...):
self.my_variable = my_variable
For example, the example at the top of the post could be simplified to
class planet_gravity:
def __init__(self, self.r, self.m):
# define variables that are functions of the input variables
self.g = 6.6E-11 * self.m/(self.r** 2)
One can consider this syntax a variation on the way function inputs traditionally work, storing the input in a variable that can be accessed by the function. With the self
keyword, the variable is stored in a class member variable instead of a local variable.
Advantages:
- Simplifies
init
statements - Eliminates multiple variables intended to store the same data
- Backward-compatible with current python code as one cannot include
self
in a function input in this way currently.
General use case:
This syntax is designed primarily for init
functions, but could be extended to all class member functions, allowing declarations like this:
def set_my_variable_to_new_value(self, self.my_variable): pass
which would be equivalent to
def set_my_variable_to_new_value(self, new_value):
self.my_variable = new_value
Explicitly specifying variable names
Given a function with signature,
def my_function(self, input1, input_2, self.my_member_variable=5):
,
it must be possible to call the function with explicit names for the inputs. The most obvious approach is like this,
my_class.my_function(
input1=3,
input2=2,
self.my_member_variable=37
)
Interested to hear what everyone thinks. I’ve always wanted something like this in python, but maybe there are good reasons why it would be worse than the existing system.
Also, my apologies if this is an old idea, but I could not find any duplicates of it. I posted part of this idea yesterday, but realized that I had not included a definition for how functions could be called with explicit variables such as f(self.my_variable=3)
and have added this to the end of the discussion.