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
#1
Hello,

I'm looking something similar like inheritance from an object (not a class). Example we have code:

class bridge():
         var=1
class ports(bridge):
         def update_var(self):
            self.var=99
         def del_var(self):
            del var
So If I create object ports and delete or update var, it will have value of 1 on that port instance. This is what I want. But problem is I'm looking to initialize a bridge object FIRST then multiple ports. Those ports will not have their own var (instance of ports), but they will inherit var from bridge. But at some point var can/will be added/updated/deleted for that instance of port.

Some time ago I was looking at the pointers in python, but found it cumbersome....

In other words: 1) we need to have one global variable. 2) that global variable is used by multiple objects 3) those multiple objects can set that variable to their local own value, but it should not affect other objects (they still see global variable's value).

This is what I have currently. The class copies value from other class. Required to create multiple copy commands to keep everything in sync when having multiple ports (ie when var on bridge changes)....
class bridge():
  var = 1
class port():
  var = bridge.var
Many thanks in advance!
Yoriz write Jan-10-2023, 08:56 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#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
#3
(Jan-10-2023, 08:55 PM)sabuzaki Wrote: This is what I have currently. The class copies value from other class.
You are confusing code that goes in the body of the class with code that must be executed by instances of the class (in the methods of the class). If you write var = Bridge.var in the body of class Port, this code will be executed only once, it will not be executed by every instance of Port.

Learn the difference between classes and instances. Variables defined in the body of the classes are class variables. They are shared by all the instances.
Reply
#4
Hi,
Yes, the above could work even without super when having one bridge. So below is more simple. Instance adds its variable and uses local version. when deleted it will use global class variable. But problem is - how to have multiple bridges with different value? So we have port1/2/3/4 created from Bridge1 with class/"global" value of 1 and we want another Bridge with ex value 2 and other ports having that 2 global value.

Let's say scenario: We have hundreds of houses each having different internet speed: 5Mbps/100Mbps/25Mbps (so we have a class for each house). Then we have rooms/people in each house. If that person/room has a faster internet speed (lets say room1 for the house5), that room/person should not use houses internet, it should use it's own faster internet.... (use room variable rather than global house variable)

class bridge:
   var = 1
class port(bridge):
   def update_var(self,v):
       self.var = v
   def delete_var(self):
       del self.var

port1 = port()
port2 = port()
print(port1.var)
print(port2.var)
port2.update_var(3)
print(port2.var)
port2.delete_var()
print(port2.var)
Reply
#5
(Jan-11-2023, 09:00 AM)sabuzaki Wrote: Let's say scenario: We have hundreds of houses each having different internet speed: 5Mbps/100Mbps/25Mbps (so we have a class for each house). Then we have rooms/people in each house. If that person/room has a faster internet speed (lets say room1 for the house5), that room/person should not use houses internet, it should use it's own faster internet.... (use room variable rather than global house variable)
In that scenario, Room/Person are not subclasses of House because their relation is not the IS-A relationship. A room is not a particular kind of house. In that case it is better to use aggregation. Each room will have a pointer to the house to which it belongs
class House:
    def __init__(self, speed):
        self.speed = speed

class Room:
    def __init__(self, house):
        self.house = house
        self._speed = 0

    @property
    def speed(self):
        return max(self._speed, self.house.speed)

    def update_speed(self, value):
        self._speed = value

    def delete_speed(self):
        self._speed = 0

h1 = House(5)
h2 = House(100)
r1 = Room(h2)
r2 = Room(h2)
print(r1.speed)
print(r2.speed)
r1.update_speed(500)
print(r1.speed)
print(r2.speed)
print(h2.speed)
r1.delete_speed()
print(r1.speed)
r1.update_speed(50)
print(r1.speed)
Output:
100 100 500 100 100 100 100
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,184 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,804 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,040 Jan-27-2023, 05:32 PM
Last Post: caslor
  Global vs. Local Variables Davy_Jones_XIV 4 2,672 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,280 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  from global space to local space Skaperen 4 2,335 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  local / global lists RedWuff 1 1,879 May-26-2020, 03:11 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 5,124 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,455 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  modifying variables in local or global space Skaperen 2 2,226 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