Difference in object ID due to import in Python

environment

windows 10 Pro version : Python 3.8 (docker)

In one line

Don't skip the story of skipping the separation of state management files.

Main subject

game.py


from status import Status

class Game():
  def __init__():
    self.status = Status.NOTHING

status.py


from enum import Enum, auto

class Status(Enum):
    NOTHING = auto()
    WAITING = auto()
    PLAYING = auto()

class GameStatus():
  def start():
    if self.game.status == Status.NOTHING:
      ...
    if self.game.status == Status.WAITING:
      ...
    if self.game.status == Status.PLAYING:
      ...

It feels like changing the process depending on the status. It feels like creating a Status as an enumeration and changing the States to WAITING or PLAYING according to the flow of the game in the initial state Status.NOTHING. The Game class of game.py imports Status, but status.py does not import it because it has Status in the same file. There is a discrepancy in the object IDs here, and only the Status.NOTHING assigned by the init function has a different object ID, and equality comparison is not established. It seems that the enum equivalent comparison refers to the object ID instead of the value.

Without skipping

gamestatus.py


from status import Status

class GameStatus():
  def start():
    if self.game.status == Status.NOTHING:
      ...
    if self.game.status == Status.WAITING:
      ...
    if self.game.status == Status.PLAYING:
      ...

status.py


from enum import Enum, auto

class Status(Enum):
    NOTHING = auto()
    WAITING = auto()
    PLAYING = auto()

By dividing the file like that and following the procedure of importing when using all files, the difference in object ID depending on the presence or absence of import is not created.

Let's divide the file properly without skipping. .. .. ..

Recommended Posts

Difference in object ID due to import in Python
[Python] Solving the import problem due to the difference in entry points
Summary of how to import files in Python 3
Object oriented in python
Convert / return class object to JSON format in Python
To flush stdout in Python
String object methods in Python
Find the difference in Python
Dynamically import scripts in Python
Login to website in Python
Speech to speech in python [text to speech]
[Python] Another way to import
How to develop in Python
Post to Slack in Python
How to import Python library set up in EFS to Lambda
How to import a file anywhere you like in Python
[Python] How to do PCA in Python
Difference between list () and [] in Python
Easy way to customize Python import
Difference between == and is in python
Working with LibreOffice in Python: import
Convert markdown to PDF in Python
How to use SQLite in Python
In the python command python points to python3.8
Try to calculate Trace in Python
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
An alternative to `pause` in Python
How to know the internal structure of an object in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Difference in how to write if statement between ruby ​​and python
I tried to implement PLSA in Python
Try logging in to qiita with Python
How to access environment variables in Python
I tried to implement permutation in Python
difference between statements (statements) and expressions (expressions) in Python
How to dynamically define variables in Python
How to do R chartr () in Python
Pin current directory to script directory in Python
Big difference in ruby, python, httpd performance
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
Send email to multiple recipients in Python (Python 3)
Convert psd file to png in Python
Sample script to trap signals in Python
I tried to implement PLSA in Python 2
How to get the date and time difference in seconds with python
To set default encoding to utf-8 in python
Create a JSON object mapper in Python
Difference between @classmethod and @staticmethod in Python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
[Python] How to use import sys sys.argv
Log in to Slack using requests in Python
How to get a stacktrace in python
How to display multiplication table in python