Skip to content

Attributes initialized as None #2

Open
@dylwil3

Description

@dylwil3

This issue is a place to discuss the convention for attributes initialized as None specifically in the situation where:

  • These attributes are not specified using arguments passed in __init__
  • These attributes cannot actually be used as type None and have to be assigned.
  • The attribute is never used in the form if self.attribute is None: ...

For concreteness, let's focus on a specific example of this. In the current Petting Zoo implementation we have:

class Entity:  # properties and state of physical world entity
    def __init__(self):
        ...
        self.max_speed = None
        ...

and then, in Simple Tag:

def make_world(self, num_good=1, num_adversaries=3, num_obstacles=2):
        ...
        world.agents = [Agent() for i in range(num_agents)]
        for i, agent in enumerate(world.agents):
            agent.adversary = True if i < num_adversaries else False
            ...
            agent.max_speed = 1.0 if agent.adversary else 1.3
            ...

Possible Proposals

  1. Do nothing (requires # pyright:ignore because type of attribute is inferred as None.).
  2. In the definition of Entity change to:
class Entity:  # properties and state of physical world entity
    def __init__(self):
        ...
        self.max_speed : bool | None = None
        ...
  1. In the definition of Entity change to:
class Entity:  # properties and state of physical world entity
    def __init__(self):
        ...
    @property
    def max_speed(self):
        if self._max_speed is None:
            raise NotImplementedError
        return self._max_speed
    @max_speed.setter
    def max_speed(self, val : float):
        self._max_speed = val
  1. Something else.

In the most recent PR (#1 ), I took option 3, but I don't have an especially strong stance.

Would love to hear any thoughts folks might have on this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions