0

This is working:

represent_dict_order = lambda self, data: self.represent_mapping('tag:yaml.org,2002:map', data.items())
yaml.add_representer(OrderedDict, represent_dict_order)

But gives me a PyCharm warning: PEP8: do not assign a lambda expression, use a def

I follow the advice, but this is not working:

def represent_dict_order(self, data):
    self.represent_mapping('tag:yaml.org,2002:map', data.items())
yaml.add_representer(OrderedDict, represent_dict_order)

I get:

yaml.emitter.EmitterError: expected NodeEvent, but got DocumentEndEvent()

I have two questions:

  1. Why is the lambda working and the def not? Aren't they supposed to be equivalent?
  2. How can I stop PyCharm complaining about this specific error? I tried preceeding the lambda with #noinspection but it is not recognized.
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • 1
    Your lambda has a return value compared to your function definition with def. – strippenzieher Dec 07 '17 at 09:54
  • @strippenzieher: ooops, implicit return, right. Embarrassing, but I'll accept an answer. – blueFast Dec 07 '17 at 09:58
  • For your second question, if you mean the PEP8 warning, you would need to ignore E731.See https://stackoverflow.com/questions/28428307/pycharms-code-style-inspection-ignore-switch-off-specific-rules – strippenzieher Dec 07 '17 at 10:02

1 Answers1

3

Lambda expressions come with an implicit return. Therefore your lambda expression is returning the return value of self.represent_mapping but your function definition is not due to the missing return.

strippenzieher
  • 316
  • 2
  • 9