I have a superclass and a subclass that need to handle their initialisation differently, based on a regular expression. See below for a working example.

```
import os
import re

class Sample:
    RE = r'(?P<id>\d+)'
    STRICT_MATCHING = False

    def __init__(self, f):
        self.file = f
        self.basename = os.path.basename(os.path.splitext(self.file)[0])

        re_ = re.compile(self.RE)
        match = re_.fullmatch if self.STRICT_MATCHING else re_.match
        self.__dict__.update(match(self.basename).groupdict())

class DetailedSample(Sample):
    RE = r'(?P<id>\d+)_(?P<dir>[lr])_(?P<n>\d+)'
    STRICT_MATCHING = True

s1 = Sample("/asdf/2.jpg")
print(s1.id)
s2 = DetailedSample("/asdfadsf/2_l_2.jpg")
print(s2.id, s2.dir, s2.n)

```

This code works but it has two drawbacks:

* The regular expression gets recompiled every time a new `Sample` is initialised.
* The `match` function cannot be called from other class methods in `Sample` (for instance, I might want to be able to check whether a file has a valid name - relative to `RE` - before initialising a `Sample` from it).

To put it simply, I'd like to have something like this:

```
class Sample:
    RE = r'(?P<id>\d+)'
    STRICT_MATCHING = False
    re_ = re.compile(RE)  #
    match = re_.fullmatch if STRICT_MATCHING else re_.match  #

    def __init__(self, f):
        self.file = f
        self.basename = os.path.basename(os.path.splitext(self.file)[0])

        self.__dict__.update(self.match(self.basename).groupdict())

    @classmethod
    def valid(cls, f):
        basename, ext = os.path.splitext(os.path.basename(f))
        return cls.match(basename) and ext.lower() in ('.jpg', '.jpeg', '.png')

class DetailedSample(Sample):
    RE = r'(?P<id>\d+)_(?P<dir>[lr])_(?P<n>\d+)'
    STRICT_MATCHING = True

```

This, however, obviously won't work in the subclasses, because the two lines marked with `#` won't execute after the redefinition of `RE` and `STRICT_MATCHING` in the subclass.

Is there an approach that will:

* keep the functionality of the first approach (i.e. regex-based intialisation);
* only compile the regex and define the match method once per subclass;
* allow the match method to be called from class methods;
* only require the redefinition of the regex string and `STRICT_MATCHING` parameter in the subclasses?