Python Forum
Cant access variable from anywhere
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant access variable from anywhere
#22
private variable implies a class. Python has classes. Python classes are just like C# classes. Here is a Python class based on your original post.
class SomeClass:
    def __init__(self, m, s, t, y, z=0):
        self.m = m
        self.s = s
        self.t = t
        self.y = y
        self._z = z

    @property
    def z(self):
        f = (self.m - 5) / (self.m - self._z)
        sm = self.s / f
        fi = sm + self.t
        self._z = fi / self.y
        return self._z

    @z.setter
    def z(self, value):
        self._z = value

thing = SomeClass(1, 2, 3, 4)
print(thing.z, thing.z, thing.z)

thing.y = 2
thing.z = 0
print(thing.z, thing.z, thing.z)
Output:
0.625 0.703125 0.712890625 1.25 1.5625 1.640625
As you can see from the output. z changes value each time it is accessed. Also demonstrated is how you can change attributes of the object (values used in the equations) from outside the class.
Reply


Messages In This Thread
Cant access variable from anywhere - by Frankduc - Nov-01-2022, 12:51 PM
RE: Cant access variable from anywhere - by rob101 - Nov-01-2022, 01:04 PM
RE: Cant access variable from anywhere - by rob101 - Nov-01-2022, 01:29 PM
RE: Cant access variable from anywhere - by deanhystad - Nov-03-2022, 06:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 2,251 Jul-12-2024, 09:36 AM
Last Post: CAD79
  Can we access instance variable of parent class in child class using inheritance akdube 3 15,805 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Access a variable of a daemon peek_no_boo 8 4,903 Apr-03-2020, 07:29 PM
Last Post: BrendanD
  How to access class variable? instances vs class drSlump 5 4,415 Dec-11-2019, 06:26 PM
Last Post: Gribouillis
  How can I access this variable from a def? student3m 1 3,505 Sep-23-2017, 02:06 PM
Last Post: ichabod801
  Can access class private variable? Michael 2 8,215 Aug-11-2017, 01:59 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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