Skip to content

Latest commit

 

History

History
142 lines (100 loc) · 3.91 KB

File metadata and controls

142 lines (100 loc) · 3.91 KB

Pythonic Code

Writing Idiomatic Python Code (PEP 8 Guidelines)

PEP 8 is the style guide for Python code. Following PEP 8 guidelines helps ensure consistency and readability in Python codebases.

  • Naming Conventions:

    • Variables and Functions: Use snake_case.
    • Classes: Use CamelCase.
    • Constants: Use UPPERCASE_WITH_UNDERSCORES.
  • Indentation:

    • Use 4 spaces per indentation level.
  • Line Length:

    • Limit lines to 79 characters.
  • Blank Lines:

    • Surround top-level function and class definitions with two blank lines.
    • Use one blank line to separate methods within a class.
  • Imports:

    • Import standard libraries first, followed by third-party libraries, and then local imports, each group separated by a blank line.
    • Import only what you need (avoid from module import *).
  • Whitespace:

    • Avoid extraneous whitespace in expressions and statements.
    • Use a single space around operators and after commas, but not directly inside bracketing constructs.

Example adhering to PEP 8:

class MyClass:
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def display(self):
        print(f"Name: {self.name}, Value: {self.value}")

def my_function(param1, param2):
    return param1 + param2

Using List Comprehensions and Generator Expressions

List comprehensions and generator expressions are brief ways to create lists and iterators. They enhance readability and can be more efficient than traditional loops.

  • List Comprehensions:
    • Syntax: [expression for item in iterable if condition]

Example:

# Traditional loop
squares = []
for x in range(10):
    squares.append(x**2)

# List comprehension
squares = [x**2 for x in range(10)]
  • Generator Expressions:
    • Syntax: (expression for item in iterable if condition)
    • Generator expressions are similar to list comprehensions but use parentheses instead of square brackets. They generate items one at a time and are more memory-efficient.

Example:

# List comprehension
squares = [x**2 for x in range(10)]

# Generator expression
squares_gen = (x**2 for x in range(10))

# Convert generator to list
squares = list(squares_gen)

Leveraging Python’s @property Decorator for Attribute Access Control

The @property decorator in Python allows you to define methods that behave like attributes, providing controlled access to instance variables. This is useful for encapsulation and validation.

  • Basic Usage:
    • Use @property to define a getter method.
    • Use @<property_name>.setter to define a setter method.
    • Use @<property_name>.deleter to define a deleter method.

Example:

class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if not value:
            raise ValueError("Name cannot be empty")
        self._name = value

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, value):
        if value < 0:
            raise ValueError("Age cannot be negative")
        self._age = value

    @age.deleter
    def age(self):
        del self._age

# Usage:
person = Person("Alice", 30)
print(person.name)  # Output: Alice
person.age = 35
print(person.age)   # Output: 35
del person.age

Benefits of Pythonic Code

  • Readability: Code is easier to read and understand, reducing the cognitive load on developers.
  • Maintainability: Consistent and idiomatic code is easier to maintain and extend.
  • Efficiency: List comprehensions and generator expressions can be more efficient in terms of execution time and memory usage.
  • Encapsulation: Using properties helps encapsulate data, providing controlled access and validation for instance variables.

Go Back