GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Unnecessary lambda — CodeQL query help documentation

Unnecessary lambda¶

ID: py/unnecessary-lambda
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - quality
   - maintainability
   - useless-code
Query suites:
   - python-code-quality.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

A lambda that calls a function without modifying any of its parameters is unnecessary. Python functions are first class objects and can be passed around in the same way as the resulting lambda.

Recommendation¶

Remove the lambda, use the function directly.

Example¶

In this example a lambda is used unnecessarily in order to pass a method as an argument to call_with_x_squared.

import math

def call_with_x_squared(x, function):
    x = x*x
    return function(x)

print call_with_x_squared(2, lambda x: math.factorial(x))

This is not necessary as methods can be passed directly. They behave as callable objects.

import math

def call_with_x_squared(x, function):
    x = x*x
    return function(x)

print call_with_x_squared(2, math.factorial)

References¶

  • Python: lambdas.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.