#๐Ÿ”’ Conventions regarding when to make a class

44 messages ยท Page 1 of 1 (latest)

robust copper
#

Is making a class overkill if what i want is a function that stores state? How else would i go about this without other bad practices like global variables?

patent craneBOT
#

@robust copper

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.

thorny grail
#

Can you show some code?

robust copper
robust copper
#

this is because the camera "moves too fast"

#

(now obviously there are probably better solutions to this)

thorny grail
#

This seems much more fitting as a method

robust copper
#

but i am curious as to this particular case

robust copper
thorny grail
#

Well, what's camera

#

since it's not in the signature

robust copper
#

it should actually be base.camera. base is a global variable storing lots of stuff

#

this is from a template in Panda3D

#

which i am very very new to

#

for context, panda3D is a bit like Pygame except with more 3D. when rendering a scene, mouse actions by default move the scene around. but i find this motion much too fast

vague pivot
robust copper
vague pivot
#

i would either define it as a normal function or attach it to an "Environment" object

vague pivot
#

idk what your code is but you could have an object that represents the world you are programming and define methods for it

#

i have a big simulation ive made that has class Environment with methods such as get_state, reset, fast_forward etc

#

if you dont need the class just define it as a normal function

pseudo smelt
#

by "stateful function", I mean a function with persistent state between calls.

vague pivot
pseudo smelt
vague pivot
#

i interpreted state as location of entities within his game

#

but yeah a dict could work

robust copper
#

the state required is the position of the camera in the previous frame, which happens to be calculated in the previous call of the function

pseudo smelt
robust copper
vague pivot
#

which isnt terrible in pygame/python/pandas3D

robust copper
#

this seems to work

#

it's a bit odd having one instance that i never reference

#

ok (x+y)/10 is not a weighted average why is this sorta working

vague pivot
#

gonna have to define " sorta working"

robust copper
#

ok still weird but i can probably just figure the rest out myself

robust copper
#

!close

patent craneBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.