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.

Example:

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

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

# Main code 
# declare object 
p = person()
# print
p.printValues();

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

Output:

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

Leave a Comment

error: Alert: Content is protected!!