1

i am trying to assign the return values from the classmethod to the init method variables but shows the error "TypeError: cannot unpack non-iterable NoneType object"

i tried assigning to one variable and could see that it returns "tuple"

class port_mappings: ip_prefix=[] def __init__(self,node1_id,node2_id): self.node1_id=node1_id self.node2_id=node2_id self.node1_ip,self.node2_ip= port_mappings.gen_ip_prefix() @classmethod def gen_ip_prefix(cls): if len(cls.ip_prefix)==0: cls.ip_prefix.append("10.0.0.0/31") print(cls.ip_prefix) return "10.0.0.0/31","10.0.0.1/31" else: pass node_list=port_mappings("95","96") 

Since it is returning the tuple, i am trying the following but

self.node1_ip,self.node2_ip= port_mappings.gen_ip_prefix() 

it shows the following error

TypeError: cannot unpack non-iterable NoneType object

3
  • 2
    What if len(cls.ip_prefix) != 0, then your function goes into the else: pass and returns None. Commented Sep 4, 2019 at 14:44
  • Hi primusa, i have some other code to put in else block. First i need this assignment for this instance variable from the return method Commented Sep 4, 2019 at 14:47
  • Thank Primusa, for the second time initialisation it was hitting the else block. i fixed it now with some code in else block..thank you Commented Sep 4, 2019 at 15:08

2 Answers 2

1

The reason this returns None is because you're calling gen_ip_prefix twice.

Once in class initialization self.node1_ip,self.node2_ip= port_mappings.gen_ip_prefix()

And then another time in your second block of code.

The first time you call the function it sees cls.ip_prefix as []. The entry of 10.0.0.0/31 is then placed into it, resulting in ["10.0.0.0/31"].

This means the second time you call the function, it sees the length as greater than zero and goes to your else statement. Since the else statement is the end of the function (so you actually don't need that else), and no return value has been defined at that point, None is returned.

None isn't an iterable, (not a tuple, list, etc), and therefore does not contain multiple values which could be unpacked in a, b = function() type statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually you are right, it made to think that i was testing the code without else block which got me stuck in this problem. i just added some code in else and it is returning and assigning those values. thank you delyeet
1

I think the problem is that you are calling the gen_ip_prefix() method without any arguments, so your if condition: f len(cls.ip_prefix)==0: finds that the cls argument is None and then ends up in the else condition, where the function simply passes and does not return a value. This is why you get the TypeError.

3 Comments

No Rob, if you run this with one initialisation it will work fine
if you add another instance for the same class it throws the error node_list=port_mappings("95","96") print(node_list.__dict__) k=port_mappings("3","4") print(k.__dict__)
Actually you are right, it made to think that i was testing the code without else block which got me stuck in this problem. i just added some code in else and it is returning and assigning those values. thank you Rob

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.