Martin
Martin

Reputation: 77

Itertools not callable

Today i had a weird error when implementing How to get all possible combinations of a list’s elements? solution by Dan H. I keep getting this error:

TypeError: 'itertools.combinations' object is not callable

does anyone know why this is and how it can be fixed? I use the following code:

def all_subsets(ss):
           return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))

for subset in all_subsets([1,2,3,4]):

After this i get an error. Can anyone help me?

EDIT1:

People asked for the full code so here is some RELEVANT part of the code error :

from scapy.all import *
import itertools
from itertools import *

def all_combinations(input):
  return chain(*map(lambda y: combinations(input, y), range(0, len(input)+1)))

extensionHeader=["IPv6ExtHdrHopByHop","IPv6ExtHdrRouting","IPv6ExtHdrDestOpt","IPv6ExtHdrFragment"]
ip=IPv6(src="2a00:1450:4009:807::1002", dst="2a00:1450:4009:807::1002")
combinations = itertools.combinations(extensionHeader, 1)


for subset in all_combinations(extensionHeader):
    index=0
    finalForm=""
    while index<len(subset):
        substring=subset[index:index+17]
        if substring is "IPv6ExtHdrRouting":
            if index==0:
                ip.nh=43

Also here is the full error:

WARNING: No route found for IPv6 destination :: (no default route?)
Traceback (most recent call last):
  File "C:\Users\Martinooos\workspace\testing\scapyTest.py", line 18, in <module
>
    for subset in all_combinations(extensionHeader):
  File "C:\Users\Martinooos\workspace\testing\scapyTest.py", line 11, in all_com
binations
    return chain(*map(lambda y: combinations(input, y), range(0, len(input)+1)))
  File "C:\Users\Martinooos\workspace\testing\scapyTest.py", line 11, in <lambda
>
    return chain(*map(lambda y: combinations(input, y), range(0, len(input)+1)))
TypeError: 'itertools.combinations' object is not callable

Upvotes: 2

Views: 4521

Answers (1)

jh314
jh314

Reputation: 27792

EDIT:

This is your issue:

combinations = itertools.combinations(extensionHeader, 1)

You don't want to save the results into combinations, because then when you try to use the combinations() function in all_combinations(), Python will try to use your combinations variable as a function, so you will get your error:

'itertools.combinations' object is not callable

To fix, just rename your variable to something else, like:

combs = itertools.combinations(extensionHeader, 1)

or modify your all_combinations() to use itertools.combinations rather than just combinations:

def all_combinations(input):
  return itertools.chain(*map(lambda y: itertools.combinations(input, y), range(0, len(input)+1)))

Upvotes: 2

Related Questions