FastBinaryEncoding icon indicating copy to clipboard operation
FastBinaryEncoding copied to clipboard

python uuid default value small bug

Open PengyiPan opened this issue 1 year ago • 3 comments

In latest version 1.12.0.0, when generating python files

example .fbe file

message Generated
{
     string val1 = "42";
     uuid [id] = uuid1;
}

will create something like

...
class Generated(object):
     ...
     def __init__(self, val1=None, id=uuid.uuid1()):
          ...

which actually fixes the id to be the same when file is first evaluated, so the following code will print the same value

g1 = Generated()
g2 = Generated()
print(g1.id)
print(g2.id)

and I think this behaviour is inconsistent with its C++ counterpart.

Proposed fix:

...
class Generated(object):
     ...
     def __init__(self, val1=None, id=None):
          ...
          if id is None:
              id = uuid.uuid1()

This error may also appear for other type of default value, for example '[]'(empty list), etc. I have to say this python feature is quite counter-intuitive.

see also: https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument

PengyiPan avatar Sep 28 '22 12:09 PengyiPan