mirror of
https://github.com/jbranchaud/til
synced 2026-01-06 16:48:01 +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).
|
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)
|
* [MySQL](#mysql)
|
||||||
* [Phoenix](#phoenix)
|
* [Phoenix](#phoenix)
|
||||||
* [PostgreSQL](#postgresql)
|
* [PostgreSQL](#postgresql)
|
||||||
|
* [Python](#python)
|
||||||
* [Rails](#rails)
|
* [Rails](#rails)
|
||||||
* [React](#react)
|
* [React](#react)
|
||||||
* [React Native](#react-native)
|
* [React Native](#react-native)
|
||||||
@@ -516,6 +517,10 @@ _912 TILs and counting..._
|
|||||||
- [Word Count for a Column](postgres/word-count-for-a-column.md)
|
- [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)
|
- [Write A Query Result To File](postgres/write-a-query-result-to-file.md)
|
||||||
|
|
||||||
|
### Python
|
||||||
|
|
||||||
|
- [Access Instance Variables](python/access-instance-variables.md)
|
||||||
|
|
||||||
### Rails
|
### Rails
|
||||||
|
|
||||||
- [Add A Check Constraint To A Table](rails/add-a-check-constraint-to-a-table.md)
|
- [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