FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [View Raw Code]   [Original HTTPS Page]

til_example/python/access-instance-variables.md at master · aaajit/til_example · GitHub

forked from jbranchaud/til

Latest commit

 

History

History
23 lines (17 loc) · 529 Bytes

File metadata and controls

23 lines (17 loc) · 529 Bytes

Access Instance Variables

You can define instance variables when instantiating a class.

class Person:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def full_name(self):
        return self.first_name + " " + self.last_name

Then those instance variables can be accessed as properties of that class instances.

me = Person("Josh", "Branchaud")

print(me.first_name) #=> "Josh"
print(me.full_name()) #=> "Josh Branchaud"

Back | FazBrowse Home | New Git URL