Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solving a scope issue
#5
I wanted to get back to you on this.

1st, I appreciate your response. It did help me better understand what I might want to do.

I may still go this route since I would like to divide my code into smaller, more manageable pieces (whatever that may mean :( However, I came across a "solution" that may be better, for the moment. I installed an outliner add on into SublimeText3. That offers me visibility (and easier access) to the classes and methods/functions I create. How that may work as my code continues to grow remains to be seen. Maybe that's when I come back to a more modular approach and figure out how to deal with objects.

What I can do now is put variables and other definitions into modules since most of those are accessed by most, or many, of my routines.

Thanks again for the help. I'll mark this thread solved for now.

(Oct-20-2019, 05:36 PM)ichabod801 Wrote:
(Oct-20-2019, 04:25 PM)profconn1 Wrote: It seems "self" isn't being passed to def PWRToggle(self, event):

That's because when you moved it out of the class and into another module, it stopped being a bound method. Now it's just a function. You could take that function and bind it to the class when it instantiates, but that gets rather deep into the Python internals. When I find myself doing that, I ask myself if there is another way around the problem.

If you need that function to be available to other classes, it would probably be best to make a class with that function as a method, and then subclass from that. So you would have the modularized form be:

class FunctionFrame(wx.Frame):
    def self.PWRToggle(event):
        if self.pwr_btn.GetValue() == True:
            ...
And then your main class would inherit from that:

  
from other_module import FunctionFrame

class MainClassWindow(FunctionFrame):
    def __init__(self):
        ...  
If you have some other reason to be modularizing that function, the solution would depend on that reason.
Reply


Messages In This Thread
Solving a scope issue - by profconn1 - Oct-20-2019, 01:22 AM
RE: Solving a scope issue - by Gribouillis - Oct-20-2019, 07:00 AM
RE: Solving a scope issue - by profconn1 - Oct-20-2019, 04:25 PM
RE: Solving a scope issue - by ichabod801 - Oct-20-2019, 05:36 PM
RE: Solving a scope issue - by profconn1 - Nov-01-2019, 07:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,773 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 853 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,875 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,554 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 3,072 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,826 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Block of code, scope of variables and surprising exception arbiel 8 3,444 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,533 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Namespace and scope difference Uchikago 9 4,632 Jul-03-2019, 03:36 PM
Last Post: Uchikago
  what is scope for python? samexpert 2 2,258 Jun-24-2019, 01:03 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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