Python Forum
User-defined function to reset variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User-defined function to reset variables?
#1
Hi all,

A general outline of my program could be written as something like this:

Variable initiation (reset)

Control branch A

    if...

    elif...

    elif...

    else...
        ...
        Variable reset

Control branch B

    if...

    elif...

    else...
        ...
        Variable reset
Variable initiation and reset simply assign particular variables to 0, empty lists, etc. Because it's done in multiple places, I'm thinking about creating a user-defined function where the variables common to all three places are reset to 0.

If I were to do such a thing, how would I handle variable scope? The variables reset in the function need to be available throughout the main program.

Finally, do you think this is even worth doing?

I have ~50 variables in this program and maybe 30 would be reset as part of the function.
Reply
#2
(May-25-2022, 02:31 PM)Mark17 Wrote: I'm thinking about creating a user-defined function where the variables common to all three places are reset to 0.
This is why classes were invented: to group in a single entity variables shared by different places. So create a class (or several classes) to hold the variables
class Foo:
    def __init__(self):
        self.reset()

    def reset(self):
        self.baz = 10
        self.bar = 'hello'
        self.spam = 3.14159

foo = Foo()
...
...
if ...:
    ...
    foo.reset()
Reply
#3
(May-25-2022, 03:05 PM)Gribouillis Wrote: This is why classes were invented: to group in a single entity variables shared by different places. So create a class (or several classes) to hold the variables

I've studied some intro stuff about classes, but they really confuse me (along with all the syntax like the __init__, the self, etc.). Can you explain this in a simpler way? If I clearly understand their place and am convinced it's worth doing, then I'll force myself through it and figure it out.
Reply
#4
(May-25-2022, 07:14 PM)Mark17 Wrote: If I clearly understand their place and am convinced it's worth doing
Decades of progress in programming and generations of top-level engineers tell you it's worth doing. There is absolutely no reason to hesitate: learn classes as soon as possible. Start with chapter 9 of the official Python Tutorial for an introduction.
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 602 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,334 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  How to print variables in function? samuelbachorik 3 917 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,106 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Getting NameError for a function that is defined JonWayn 2 1,122 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,912 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,321 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Multiple user defined plots with secondary axes using for loop maltp 1 1,463 Apr-30-2022, 10:19 AM
Last Post: maltp
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,496 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Setting permanent user variables in Windows coder420 2 1,426 Jan-04-2022, 10:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020