# Python Style coding

# Super Quick Refactoring

Quick Python Refactoring Tips Super Quick Python Refactoring Tips

# Merge nested if statements

Code: ❌

if a:
    if b:
        pass
1
2
3

Refactoring: ✅

if a and b:
    pass
1
2

# Use any() instead of a loop

any() definition:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
1
2
3
4
5

Code: ❌

numbers = [-1, -2, -4, 0, 3, -7]
has_positives = False
for n in numbers:
    if n > 0:
        has_positives = True
        break
1
2
3
4
5
6

Refactoring: ✅

numbers = [-1, -2, -4, 0, 3, -7]
has_positives = any(n > 0 for n in numbers)
1
2

# Pull statements out of loops

Code: ❌

for building in buildings:
    city = 'London'
    address.append(building.street_address, city)
1
2
3

Refactoring: ✅

city = 'London'
for building in buildings:
    address.append(building.street_address, city)
1
2
3

# Remove inline variables that are only used once

Code: ❌

def state_attibutes(self):
    """Return the state attributes."""
    state_attr = {
        ATTR_CODE_FORMAT: self.code_format,
        ATTR_CHANGED_BY: self.changed_by,
    }
    return state_attr
1
2
3
4
5
6
7

Refactoring: ✅

def state_attibutes(self):
    """Return the state attributes."""
    return = {
        ATTR_CODE_FORMAT: self.code_format,
        ATTR_CHANGED_BY: self.changed_by,
    }
1
2
3
4
5
6

# Replace an if statement with an if expression

Code: ❌

if condition:
    x = 1
else:
    x = 2
1
2
3
4

Refactoring: ✅

x = 1 if condition else 2
1

# Add a guard clause (+ Remove unnecessary else)

Code: ❌

def shoul_i_wear_this_hat(self, hat):
    if isinstance(hat, Hat):
        current_fashion = get_fashion()
        weather_outside = self.look_out_of_window()
        is_stylish = self.evaluate_style(hat, current_fashion)
        if weather_outside.is_raining:
            print("Damn.")
            return True
        else:
            print("Great.")
            return is_stylish
    else:
        return False
1
2
3
4
5
6
7
8
9
10
11
12
13

Refactoring: almost ✅

def shoul_i_wear_this_hat(self, hat):
    if not isinstance(hat, Hat):
        return False
        
    current_fashion = get_fashion()
    weather_outside = self.look_out_of_window()
    is_stylish = self.evaluate_style(hat, current_fashion)
    if weather_outside.is_raining:
        print("Damn.")
        return True
    else:
        print("Great.")
        return is_stylish   
1
2
3
4
5
6
7
8
9
10
11
12
13

# Move assignments closer

Code: ❌

def shoul_i_wear_this_hat(self, hat):
    if not isinstance(hat, Hat):
        return False
        
    current_fashion = get_fashion()
    weather_outside = self.look_out_of_window()
    is_stylish = self.evaluate_style(hat, current_fashion)
    if weather_outside.is_raining:
        print("Damn.")
        return True
    else:
        print("Great.")
        return is_stylish 
1
2
3
4
5
6
7
8
9
10
11
12
13

Refactoring: ✅

def shoul_i_wear_this_hat(self, hat):
    if not isinstance(hat, Hat):
        return False
        
    weather_outside = self.look_out_of_window()
    if weather_outside.is_raining:
        print("Damn.")
        return True
    else:
        print("Great.")
        current_fashion = get_fashion()
        is_stylish = self.evaluate_style(hat, current_fashion)
        return is_stylish  
1
2
3
4
5
6
7
8
9
10
11
12
13

# Simplify Sequence checks

Code: ❌

if len(list_of_hats) > 0:
    hat_to_wear = choose_hat(list_of_hats)
1
2

Refactoring: ✅

if list_of_hats:
    hat_to_wear = choose_hat(list_of_hats)
1
2

# if currency in ["USD", "EUR"]

Code: ❌

def process_payment(payment, currency):
    if currency == "USD" or currency == "EUR":
        process_standard_payment(payment)
    else:
        process_international_payment(payment)
1
2
3
4
5

Refactoring: ✅

def process_payment(payment, currency):
    if currency in ["USD", "EUR"]:
        process_standard_payment(payment)
    else:
        process_international_payment(payment)
1
2
3
4
5