Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

vars() python

# function vars() is defined as: vars([object]) -> dictionary
# the vars() function takes an object and returns a its __dict__ attribute,
# which is a dictionary of all of its writable attributes

class Person:
	def __init__(self, name = "anonymous", age = 21, employed = True):
		self.name = name
		self.age = age

        
Sam = Person()
John = Person('John', 18)

# notice the 'employed' attribute is not returned
print(vars(Sam))  # {'name': "anonymous", 'age': 21}
print(vars(John))  # {'name': "John", 'age': 18}
Source by docs.python.org #
 
PREVIOUS NEXT
Tagged: #python
ADD COMMENT
Topic
Name
6+7 =