too-many-locals / R0914¶
Message emitted:
Too many local variables (%s/%s)
Description:
Used when a function or method has too many local variables.
Problematic code:
from childhood import Child, Sweet
def handle_sweets(infos): # [too-many-locals]
# Create children
children = [Child(info) for info in infos]
number_of_sweets = 87
sweets = [Sweet() * number_of_sweets]
number_of_sweet_per_child = 5
money = 45.0
sweets_given = 0
time_to_eat_sweet = 54
price_of_sweet = 0.42
# distribute sweet
for child in children:
sweets_given += number_of_sweet_per_child
child.give(sweets[number_of_sweet_per_child:])
# calculate prices
cost_of_children = sweets_given * price_of_sweet
# Calculate remaining money
remaining_money = money - cost_of_children
# Calculate time it took
time_it_took_assuming_parallel_eating = (
time_to_eat_sweet * number_of_sweet_per_child
)
print(
f"{children} ate {cost_of_children}¤ of sweets in {time_it_took_assuming_parallel_eating}, "
f"you still have {remaining_money}"
)
Correct code:
from typing import NamedTuple
from childhood import Child, Sweet
class SweetDistrubutionCharacteristics(NamedTuple):
number_of_sweets: int
number_of_sweet_per_child: int
number_of_children: int
@property
def sweets_given(self):
return self.number_of_sweet_per_child * self.number_of_children
def handle_sweets(infos):
children = [Child(info) for info in infos]
characteristics = SweetDistrubutionCharacteristics(87, 5, len(children))
_allocate_sweets_to_children(children, characteristics)
financial_impact = _assess_financial_impact(characteristics)
print(f"{children} ate {financial_impact}")
def _allocate_sweets_to_children(
children, characteristics: SweetDistrubutionCharacteristics
) -> None:
sweets = [Sweet() * characteristics.number_of_sweets]
for child in children:
child.give(sweets[characteristics.number_of_sweet_per_child :])
def _assess_financial_impact(characteristics: SweetDistrubutionCharacteristics) -> str:
time_to_eat_sweet = 54
money = 45.0
price_of_sweet = 0.42
cost_of_children = characteristics.sweets_given * price_of_sweet
remaining_money = money - cost_of_children
time_it_took_assuming_parallel_eating = (
time_to_eat_sweet * characteristics.number_of_sweet_per_child
)
return (
f"{cost_of_children}¤ of sweets in "
f"{time_it_took_assuming_parallel_eating}, you still have {remaining_money}"
)
Configuration file:
[design]
max-locals = 11
Additional details:
Having too many locals may indicate that you're doing too much in a function and that classes regrouping some attributes could be created. Maybe operations could be separated in multiple functions. Are all your variables really closely related ?
Created by the design checker.