Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes initialized as None #2

Open
dylwil3 opened this issue Jun 11, 2023 · 0 comments
Open

Attributes initialized as None #2

dylwil3 opened this issue Jun 11, 2023 · 0 comments

Comments

@dylwil3
Copy link
Collaborator

dylwil3 commented Jun 11, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant