Created
February 27, 2018 19:47
-
-
Save sabinem/ad11a3dca50d0f88e35788a5e52ef9a3 to your computer and use it in GitHub Desktop.
Using vars() in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| This is a simple example on how to use vars() in python: | |
| vars() is for getting the variables from objects that have a __dict__ | |
| (this is built for python 3.6, you can copy this into a Jupyter notebook and execute!) | |
| """ | |
| # vars is for getting variables from an objects __dict__ | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __repr__(self): | |
| return "({},{})".format(self.x, self.y) | |
| p = Point(2,3) | |
| P = Point | |
| print("instance dictionary: Point(2,3).__dict__={}".format(Point(2,3).__dict__)) | |
| print("this is the same as vars(Point(2,3))={}\n".format(vars(Point(2,3)))) | |
| print("class dictionary: Point.__dict__={}".format(Point.__dict__)) | |
| print("this is the same as vars(Point)={}\n".format(vars(Point))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment