Python Forum
How to use global value or local value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use global value or local value
#2
I don't really understand your question. Is this applicable?
class Bridge:
    var = 1


class Port(Bridge):
    def __init__(self, var=None):
        super().__init__()
        self.var = super().var if var is None else var

    def super_var(self):
        return super().var


a = Port()
b = Port(2)
Bridge.var = 3
c = Port()
d = Port(4)

print(a.var, b.var, c.var, d.var, a.super_var())
Output:
1 2 3 4 3
All ports share the same Bridge.var class variable. The class variable can be assigned different values using the class, not an instance of the class.

All ports have an instance variable named "var". This variable is unique to the instance. Changing the instance variable does not change the class variable, and changing the class variable does not change the instance variable. The instance variable masks the class variable but does not replace it. Instances can still see the class variable though a little extra work is required.
Reply


Messages In This Thread
RE: How to use global value or local value - by deanhystad - Jan-10-2023, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,319 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,982 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,116 Jan-27-2023, 05:32 PM
Last Post: caslor
  Global vs. Local Variables Davy_Jones_XIV 4 2,733 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,404 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  from global space to local space Skaperen 4 2,405 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  local / global lists RedWuff 1 1,931 May-26-2020, 03:11 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 5,271 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,547 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  modifying variables in local or global space Skaperen 2 2,299 Aug-14-2019, 07:13 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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