1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00

Add Access Instance Variables as a python til

This commit is contained in:
jbranchaud
2020-05-13 11:09:18 -05:00
parent e120f3a10b
commit 791ed4e23f
2 changed files with 29 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
# Access Instance Variables
You can define instance variables when instantiating a class.
```python
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.
```python
me = Person("Josh", "Branchaud")
print(me.first_name) #=> "Josh"
print(me.full_name()) #=> "Josh Branchaud"
```