Answers for "python variabele class"

3

class python

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40
Posted by: Guest on October-29-2020
1

class variable in python

# Class variables refer to variables that are made within a class.
# It is generated when you define the class.
# It's shared with all the instance of that class.
# Example:

class some_variable_holder(object):
    
    var = "This variable is created inside the class some_variable_holder()."
    
    def somefunc(self):
        print("Random function ran.")

thing = some_variable_holder()
another_thing = some_variable_holder()

# Both does the same thing because the same variable has been passed on from the class.
thing.var
another_thing.var
Posted by: Guest on August-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language