Function default argument: comparison with None?

Hello,
Imagine the following simple function that result depend on an optional arg

def fnt(a,b,c=None):
  if c != None:
    res = a+b+c
  else:
   res = a+b
 return res

The point is that if cis set to a torch tensor as aand `b, then an exception is rised.

TypeError: ne() received an invalid combination of arguments - got (NoneType), but expected one of:
 * (Tensor other)
      didn't match because some of the arguments have invalid types: (NoneType)
 * (Number other)
      didn't match because some of the arguments have invalid types: (NoneType)

Do you know how to manage the possibility to get c as option?
(nb. I have swap the if condition according to @Deepali suggestion even if the problem remains)

I think you want to swap the if conditions:

def fnt(a,b,c=None):
  if c != None:
    res = a+b+c  //c is not None, so use it. 
  else:
   res = a+b  //forget c as it is None
 return res

Also use torch.add method to add the tensors.

Sorry,
The fnt I consider in the example is simple and you’re right in the philosophy.

But it remains that the c != None cannot apply. c is a torch.tensor of shape (N,1) where N is the batch size.

a= torch.Size([100, 1])
 if a != None:
    print(a)
 else:
    print('nok')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ne() received an invalid combination of arguments - got (NoneType), but expected one of:
 * (Tensor other)
      didn't match because some of the arguments have invalid types: (NoneType)
 * (Number other)
      didn't match because some of the arguments have invalid types: (NoneType)

yes should have mentioned that too.
Pls change comparison to if c is None:

Thanks @Deepali
is None works.