#πŸ”’ how do i fix my AWFUL code presentation

46 messages Β· Page 1 of 1 (latest)

foggy crag
#

so i'm making physics in pygame and im making a class called physics object, and theres a lot of variables that are closely linked and idk how to seperate them so now my code looks like garbage

hearty swanBOT
#

@foggy crag

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dawn wadi
#

You could package things like coordinates into tuples

#

like when you create a physics object, you could pass in (x, y) for the x and y position

#

but honestly I don't think it looks bad

left viper
#

I think the name is worse than the initializer body.

foggy crag
foggy crag
#

or should i do lists

#

cuz i cant really edit tuples

brazen epoch
#

doesn't really matter as you can unpack the arguments

foggy crag
#

oh

#

oh yeah i can

#

but its probably easier to directly edit the list than create new variables to unpack the tuple into

#

and update the tuple

dawn wadi
#

what I am saying is this

#

you have xpos and ypos as parameters

#

instead you can just have pos_tuple

foggy crag
#

ohh

dawn wadi
#

then you do self.xpos = pos_tuple[0]

foggy crag
#

i get it

dawn wadi
#

not sure if thats considered better or not

foggy crag
#

i mean

brazen epoch
#

if you think it will be more common for you to set the density then the gravity i would suggest to have the two switch order in the signature of the __init__ function

foggy crag
#

ok

#

ill apply those changes rn

brazen epoch
#

do you know how to use type hints?
i think that could be helpful here

foggy crag
#

no

#

never heard of it

#

wait

#

oh

#

you meant the colon?

#

like x : int

#

?

brazen epoch
#

yeah, especially for the signature of the functions

foggy crag
#

ill consider it

#

but it might make the parameters list too long

brazen epoch
#

with that many parameters you probably will need to reformat it anyways

#

ruff is a great tool to both check your code for potential problems and areas of improvement as well as let it format the code for you in a standard way

foggy crag
#

idk what it is

#

ill check it out tomorrow

#

now i should probably get to sleep

#

im absolutely washed

brazen epoch
hearty swanBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.