2

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
Community
  • 1
  • 1
Martin
  • 79
  • 3
  • 12
  • How does your code continue? I could reproduce the same error with this code: `combinations(range(3), 2)(3) -> TypeError: 'itertools.combinations' object is not callable`. A combinations object can't be called. You can only iterate over it or apply something like list() on it. – sjakobi Jul 20 '13 at 03:37
  • Can you post a full stack trace? – user2357112 Jul 20 '13 at 03:38
  • This code is working for me. Are you sure you didn't accidentally use the name `combinations` for something else and replace the `combinations` imported from `chain`? – user2357112 Jul 20 '13 at 03:43
  • Hi @Martin the `return` of your function just saved me from asking a question. Cheers! – Andy K Apr 15 '16 at 09:46

1 Answers1

2

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)))
jh314
  • 27,144
  • 16
  • 62
  • 82
  • He has to have imported it already. Otherwise, he'd be getting a `NameError` instead of successfully constructing `itertools.combinations` objects and then trying to call them as functions somewhere. – user2357112 Jul 20 '13 at 03:39
  • After OP posted entire code, I've updated my answer accordingly. – jh314 Jul 20 '13 at 04:44