Going through some old code, I found this function.
Given a string carrier
and integer nsyms
, it consumes nsyms
distinct characters from the carrier
string and returns a dictionary containing consumed characters and their respective counts, and the remainder of the carrier
string.
def enc_consume(carrier, nsyms):
chrs = defaultdict(int)
i = 0 # see this?
for i,c in enumerate(carrier):
# `i` is not used in the loop
chrs[c] += 1
if len(chrs) == nsyms:
break
return chrs, carrier[i+1:] # but it's used here
How would you rewrite this? I found the assignment i = 0
confusing, since it was followed by a for i...
, which will of course do the assignment as well.
i = 0
is effectively pointless because the name is reassigned on the next line. – Luke Sawczak 15 hours agoi
would be out of scope by the time it is used inreturn
. It's a local variable and its scope is limited to thefor
statement. After thefor
, it doesn't exist anymore. – kyrill 15 hours agofor
loop variables, but it doesn't. – Luke Sawczak 13 hours ago