code: def __generate_method(name):
          
          try:
              func = getattr(DataFrame, name)
          except AttributeError as e:
              # PySpark version is too old
              def func(self, *args, **kwargs):
                  raise e
              return func
          wraps = getattr(functools, "wraps", lambda _: lambda f: f)  # py3.4+
  
          @wraps(func)
          def _wrapper(self, *args, **kwargs):
              dataframe = func(self, *args, **kwargs)
              if self.__class__ != SourcedDataFrame \
                      and isinstance(self, SourcedDataFrame) \
                      and isinstance(dataframe, DataFrame):
                  return self.__class__(dataframe._jdf, self._session, self._implicits)
              return dataframe
  
          return _wrapper
docs: Wraps the DataFrame's original method by name to return the derived class instance.