Search code examples
pythonprotocol-buffers

How to unset a protocol buffer field?


How would you unset a protocol buffer variable.

Given this .proto:

message Product {
    optional float price = 5;
}

If one tries deleting the item:

del product.price

this results:

AttributeError: can't delete attribute

If one tries setting the variable to None:

product.price = None

this results:

TypeError: Cannot set mypackage.Product.price_was to None: None has type <class 'NoneType'>, but expected one of: numbers.Real

Solution

  • You can use ClearField like

    product.ClearField('price')
    

    Full docs