0

I recently came across with a python snippet that would create a different object model. Part of that code countians __slots__ with __fields__.

class Reptor(object):
    __slots__ = __fields__ = 'type', 'code', 'deviated_angle'

__slots__ would creates tuple object instead of dictionary object which minimize the object size and add the immutability to it. But I can't understand what's __fields__ part in here?

And __fields__ has been used in __repr__

def __repr__(self):
    fields = ', '.join(repr(getattr(self, f)) for f in self.__fields__)
    return f'(type(self).__name__)({fields})'
Govinda Malavipathirana
  • 1,095
  • 2
  • 11
  • 29

1 Answers1

1
__slots__ = __fields__ = 'type', 'code', 'deviated_angle'

__fields__ is also set here. This is basically shorthand for:

__fields__ = 'type', 'code', 'deviated_angle'
__slots__ = __fields__

__fields__ has no special meaning in Python, so it's probably used by other parts of your application.

rickdenhaan
  • 10,857
  • 28
  • 37
  • 4
    Incorrect, `__slots__` is special just not `__fields__` https://stackoverflow.com/questions/472000/usage-of-slots – Chris_Rands Dec 20 '19 at 09:21