1

I'm learning spark with python, I came up with this method,

def convert_to_row(d: dict) -> Row:
    return Row(**OrderedDict(sorted(d.items())))

this method takes a line and convert into Row.

can someone simplifies this what are these points mean.

1) d: dict

2) -> Row

3) why is ** there? for kvargs?

also, if I wrap this into the class, first argument will be self, something like that.

def convert_to_row(self, d: dict) -> Row:
    return Row(**OrderedDict(sorted(d.items())))

will it work the same way as it was doing before?

thanks.

Mubin
  • 4,325
  • 5
  • 33
  • 55
  • Related: [What are Type hints in Python 3.5](https://stackoverflow.com/q/32557920/1639625) And yes, those `**` are for `kwargs` unpacking. – tobias_k May 30 '17 at 13:51

1 Answers1

1

1) and 2) are type hints, which:

  • help other developers to understand what are the expected types of parameters/variables and what is the return type of functions
  • can be checked by mypy, a static type checker for Python

3) ** allows you to unpack each pair of key/value from the OrderedDict

Finally, wrapping this function into a class by just adding self will work.

filaton
  • 2,257
  • 17
  • 27