0

I am using Python3 sqlalchemy to do some sql query, the code look like this:

def select(self) -> any:
    with dict_session_scope() as local_session:
        word = None
        try:
            word = local_session.query(Dict) \
                .filter(Dict.demo_sentence == 0) \
                .limit(5) \
                .all()
        except SQLAlchemyError as e:
            local_session.rollback()
            logger.error("query dict word error", e)
        finally:
            local_session.close()
        return word

I was wonder is it possible to avoid add a slash \ in each lambda expression end. if I write the code with one line, sometimes it is too long and hard to read, if I wrap to multiline, I have to add the \ and make the code look like ugly. is it a perfect way to avoid the puzzle?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • 2
    BTW, there are no lambdas in your code. I think you're talking about the [attribute references](https://docs.python.org/3/reference/expressions.html#attribute-references), which probably point to methods. – wjandrea Oct 23 '21 at 04:20
  • In Python, lambda expressions are anonymous functions declared with the `lambda` keyword, e.g. `lambda x, y: x + y`, which is missing in your code. – rpatel Oct 23 '21 at 04:22
  • 1
    What you have separated by \ is known as "method chaining". – sj95126 Oct 23 '21 at 04:25

0 Answers0