Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class variables
#1
Given the scenario
class SomeClass:
    myvar = 0
    def __init__(self):
        do stuff here
    def func(self):
        do stuff here
        myvar += 1
        print(myvar)
Would not myvar be a global for the class?
I'm getting that it has not been assigned in the function.
Input much appreciated
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
(Jun-04-2020, 12:53 PM)menator01 Wrote: Given the scenario
class SomeClass:
    myvar = 0
    def __init__(self):
        do stuff here
    def func(self):
        do stuff here
        myvar += 1
        print(myvar)
Would not myvar be a global for the class?
I'm getting that it has not been assigned in the function.
Input much appreciated

I'd say that you haven't received a reply because your question obfuscates your problem. You don't have an example of usage or any error code. That aside I have a bit of advise on your problem.

Class variables are accessed the same way that instance variables are accessed, that is by way of an instance.

class SomeClass:
    myvar = 0
    def __init__(self):
        do stuff here
    def func(self):
        do stuff here
        myvar += 1
        print(myvar)

my_instance = SomeClass()
print(my_instance.myvar) 
#Unlike some other languages class variables in python are not accessed by the class name
Reply
#3
Here is an example of accessing the class variable and the instance variable
class SomeClass:
    myvar = 0

    def __init__(self):
        self.myvar = 10

    def increase_class_variable(self):
        SomeClass.myvar += 1
        print(f'Shared class variable: {SomeClass.myvar}')

    def increase_instance_variable(self):
        self.myvar += 1
        print(f'Instance variable: {self.myvar}')


instance1 = SomeClass()
instance2 = SomeClass()
instance1.increase_class_variable()
instance1.increase_class_variable()
instance2.increase_class_variable()
instance1.increase_instance_variable()
instance1.increase_instance_variable()
instance2.increase_instance_variable()
Output:
Shared class variable: 1 Shared class variable: 2 Shared class variable: 3 Instance variable: 11 Instance variable: 12 Instance variable: 11
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unchangeable variables in a class? Calab 12 1,419 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,780 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 10,391 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,867 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Question about naming variables in class methods sShadowSerpent 1 1,961 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Understanding Class Variables vindo 9 3,994 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  What is the strategy for working with class variables? AlekseyPython 3 2,948 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Base class variables are not accessible Prabakaran141 3 2,768 Oct-31-2018, 07:13 AM
Last Post: buran
  Class Modules, and Passing Variables: Seeking Advice Robo_Pi 21 10,088 Mar-02-2018, 05:22 PM
Last Post: snippsat
  Running Class methods in a loop and updating variables. ujjwalrathod007 3 6,266 Oct-05-2016, 07:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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