mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Access Instance Variables as a python til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||
|
||||
_912 TILs and counting..._
|
||||
_913 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -32,6 +32,7 @@ _912 TILs and counting..._
|
||||
* [MySQL](#mysql)
|
||||
* [Phoenix](#phoenix)
|
||||
* [PostgreSQL](#postgresql)
|
||||
* [Python](#python)
|
||||
* [Rails](#rails)
|
||||
* [React](#react)
|
||||
* [React Native](#react-native)
|
||||
@@ -516,6 +517,10 @@ _912 TILs and counting..._
|
||||
- [Word Count for a Column](postgres/word-count-for-a-column.md)
|
||||
- [Write A Query Result To File](postgres/write-a-query-result-to-file.md)
|
||||
|
||||
### Python
|
||||
|
||||
- [Access Instance Variables](python/access-instance-variables.md)
|
||||
|
||||
### Rails
|
||||
|
||||
- [Add A Check Constraint To A Table](rails/add-a-check-constraint-to-a-table.md)
|
||||
|
||||
23
python/access-instance-variables.md
Normal file
23
python/access-instance-variables.md
Normal 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"
|
||||
```
|
||||
Reference in New Issue
Block a user