0

I have a game class and I need to pass screen width and height into pygame.display.set_mode() in __init__ method. It requires a tuple, for example - pygame.display.set_mode((width, height)).

I have also a second class named Settings from which I import width and height.

If I need to pass long name attributes inside a tuple, is it okay to write these double parentheses the way I did below not to exceed 79 chars? I'm not sure because I've never seen the double parentheses case in PEP8, only single.

def __init__(self):
    """Initialize the game, create game resources."""
    pygame.init()
    self.settings = Settings()
    self.screen = pygame.display.set_mode((
        self.settings.screen_width,
        self.settings.screen_height
    ))
TylerH
  • 20,799
  • 66
  • 75
  • 101
Local man
  • 19
  • 5
  • 1
    This is opinion-based. If you think it's readable then use it. – Matthias Aug 14 '23 at 11:52
  • 1
    Does the automated pep-8 checker flag this as a violation? (If not: Is there any other concrete, practical reason you're concerned about this being a convention violation?) – Charles Duffy Aug 14 '23 at 11:56
  • 2
    In general, if you want your code formatting to follow purely mechanical rules, I would suggest using [black](https://github.com/psf/black) to enforce that. – Charles Duffy Aug 14 '23 at 11:59
  • @CharlesDuffy, I'm new to Python and I'm using PyCharm. If by pep8 checker you mean yellow underline, then no, it doesn't flag this as a violation. – Local man Aug 14 '23 at 12:00
  • 1
    I don't think double parentheses are violating PEP 8 but if you want to be sure, you can create a variable `rect` and use it like `pygame.display.set_mode(rect)` – Aemyl Aug 14 '23 at 12:00
  • 1
    I meant the command line tool named `pep8`. PyCharm may or may not integrate that tool; I don't use PyCharm -- but see https://pypi.org/project/pep8/ – Charles Duffy Aug 14 '23 at 12:01
  • The `pep8` tool has been renamed to `pycodestyle`. But it does not return any violation for this piece of code – Noé Duruz Aug 14 '23 at 12:14
  • 1
    @Matthias This is a question about compliance with a style guide, which presumably has a published preference. It is not opinion-based to ask about style guide compliance. – TylerH Aug 14 '23 at 13:57

1 Answers1

1

You can use pycodestyle to check that your code is PEP8 compliant.

If I copy-paste your code in a settings.py file, I can use

pip install pycodestyle
pycodestyle --show-source --show-pep8 settings.py

which does not return anything, so you're good.

However, as mentioned by CharlesDuffy, I suggest you to use a code formatter such as black to ensure consistent and PEP8 compliant code formatting.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Noé Duruz
  • 96
  • 3