Compare commits
7 Commits
bc3b595cc3
...
a55856bf89
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a55856bf89 | ||
|
|
b63b7f6da1 | ||
|
|
510182cb39 | ||
|
|
7c771bd014 | ||
|
|
2d7dfa6718 | ||
|
|
0cfde115ff | ||
|
|
9e290fd1a8 |
@@ -61,8 +61,9 @@ and class definitions:
|
||||
|
||||
```python
|
||||
>>> def _init(locs):
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
|
||||
>>> class Stock:
|
||||
@@ -96,8 +97,9 @@ frame hacking. Try this variant of the `_init()` function:
|
||||
>>> import sys
|
||||
>>> def _init():
|
||||
locs = sys._getframe(1).f_locals # Get callers local variables
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
>>>
|
||||
```
|
||||
@@ -135,8 +137,9 @@ class Structure:
|
||||
@staticmethod
|
||||
def _init():
|
||||
locs = sys._getframe(1).f_locals
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
...
|
||||
```
|
||||
|
||||
@@ -84,7 +84,7 @@ class Stock(Structure):
|
||||
Stock.create_init()
|
||||
```
|
||||
|
||||
The resulting class should work exactly the name way as before:
|
||||
The resulting class should work exactly the same way as before:
|
||||
|
||||
```python
|
||||
>>> s = Stock(name='GOOG', shares=100, price=490.1)
|
||||
|
||||
@@ -124,7 +124,7 @@ class Stock:
|
||||
```
|
||||
|
||||
Note: This part doesn't involve a lot of code, but there are a lot of low-level
|
||||
fiddly bits. The solution will look almost the same as for Exercise 6.6. Don't
|
||||
fiddly bits. The solution will look almost the same as for Exercise 6.5. Don't
|
||||
be shy about looking at solution code though.
|
||||
|
||||
\[ [Solution](soln7_1.md) | [Index](index.md) | [Exercise 6.5](ex6_5.md) | [Exercise 7.2](ex7_2.md) \]
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
# Exercise 1.1 - Solution
|
||||
|
||||
Nothing here. Just follow along with the exercise.
|
||||
Check [here](../Solutions/1_1/art.py)
|
||||
|
||||
|
||||
[Back](ex1_1.md)
|
||||
|
||||
@@ -11,8 +11,9 @@ class Structure:
|
||||
@staticmethod
|
||||
def _init():
|
||||
locs = sys._getframe(1).f_locals
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
|
||||
@@ -12,8 +12,9 @@ class Structure:
|
||||
@staticmethod
|
||||
def _init():
|
||||
locs = sys._getframe(1).f_locals
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
|
||||
@@ -8,8 +8,9 @@ class Structure:
|
||||
@staticmethod
|
||||
def _init():
|
||||
locs = sys._getframe(1).f_locals
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
|
||||
@@ -9,8 +9,9 @@ class Structure:
|
||||
@staticmethod
|
||||
def _init():
|
||||
locs = sys._getframe(1).f_locals
|
||||
self = locs.pop('self')
|
||||
self = locs['self']
|
||||
for name, val in locs.items():
|
||||
if name == 'self': continue
|
||||
setattr(self, name, val)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
|
||||
Reference in New Issue
Block a user