Public variables in Python

Python open factors: In this article, we will find out about the open factors in Python with Example.

As a matter, of course, all numbers, strategies, factors of the class are open in the Python programming language; we can get to them outside of the class utilizing the item name.

Think about the given example:

Here, we have two factors name and age both are instating with default esteems (“XYZ” and 0) while pronouncing the article utilizing init technique.

Afterwards, in the program, outside of the class definition – we are doling out certain qualities (“Kishan” and 19) to name and age, that is conceivable just if factors are open.


# Python example for public variables 
class person:
	def __init__(self):
		# default values = "XYZ"
		self.age = 0

	def printValues(self):
		print "Name: ", 
		print "Age : ",self.age

# Main code 
# declare object 
p = person()
# print

# since variables are public by default 
# we can access them directly here = "Kishan"
p.age = 19
# print
p.printValues ();


Name:  XYZ
Age :  0
Name:  Kishan
Age :  19

Leave a Comment

error: Alert: Content is protected!!