In both Perl and C++ one has an option to write class method definitions by defining the method outside the class and thereby avoiding en extra level of indentation.
Of course those language are free form (in contrast to Python) so you do not strictly need to add indentation for each nesting level, but one usually does so for readability.
For example, in C++ the following class definition (approach #1):
class X {
void x_method(int a, int b) {
// implementation details follows here.. Note: extra indentation
}
};
can also be written with the method definition outside the class (approach #2):
class X {
void x_method(int a, int b);
};
void X::x_method(int a, int b) {
// implementation details follows here
}
In Perl, you could write the first version as:
package X {
sub x_method {
my ($a, $b) = @_;
# implementation details follows here
}
}
and you can avoid the extra indentation for the method definition by using this style:
package X;
sub x_method {
my ($a, $b) = @_;
# implementation details follows here
}
package Y; # marks the start of another class
# ...
In these examples I used a small class definition that extends less than the amount of lines that can be viewed on a computer screen (let's say less than 50 lines). For such definitions, approach 1 with the method inside the class is fine. However, once the class definition becomes hundreds of lines, I would prefer approach #2 by defining the class methods outside the class.
However, I cannot find this approach in Python. How can I do appraoch #2 in Python?
It seems to me that you only have approach #1 in Python:
class X:
def x_method(a, b):
//implementation details follows here
I would like to have something similar to this (approach #2):
class X:
def x_method(a, b):
# implementation details..
class Y: # marks the start of another class
# ...
When the number of methods (and lines) in a class definition increases, I think appraoch #1 becomes more and more verbose. Then, the only purpose I can see that the extra space (indentation) has, is to indicate that a method belongs to a given class X. And that information (that a method belongs to a given class), does not need to expand and propagate over more and more lines as class definition grows. It is quite sufficient to contain that information to a single line as in Perl (using the package statement), or to a single prefix word as in C++ using the namespace resulution prefix operator on the method name in the first line of its definition.
I am not so familiar with Python, and coming from these other lanuages it feels like I am suffocating in redundant space (using approach #1) for large class definitions, and it distracts me from my programming tasks. What am I missing here?
Extra indentation also forces you to break more lines to keep within the recommended 80 characters per line limit.
You can't have approach #1 in python because in addition to classes there may be top level function definitions so python will not be able to find out if global_function in the example below is really global or belongs to class X:
class X:
def x_method(a, b):
# implementation details..
def global_function(x):
# implementation
class Y: # marks the start of another class
# ...
There's however one way to decrease indentation like this (but it pollutes global namespace with functions that belong to the class):
def x_method(self, b):
#implementation
class X:
x_method = x_method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With