Browse Source

Metaprogramming

pull/3/head
Jure Šorn 6 years ago
parent
commit
9be87dcfa4
1 changed files with 7 additions and 7 deletions
  1. 14
      README.md

14
README.md

@ -628,22 +628,22 @@ Introspection and Metaprograming
```python ```python
>>> class B:
>>> class Z:
... def __init__(self): ... def __init__(self):
... self.a = 'abcde' ... self.a = 'abcde'
... self.b = 12345 ... self.b = 12345
>>> b = B()
>>> z = Z()
``` ```
### Getattr, Hasattr, Setattr ### Getattr, Hasattr, Setattr
```python ```python
>>> getattr(b, 'a') # Same as B.__getattribute__(b, 'a')
>>> getattr(z, 'a') # Same as Z.__getattribute__(z, 'a')
'abcde' 'abcde'
>>> hasattr(b, 'c')
>>> hasattr(z, 'c')
False False
>>> setattr(b, 'c', 10)
>>> setattr(z, 'c', 10)
``` ```
### Type ### Type
@ -653,8 +653,8 @@ type(class_name, parents<tuple>, attributes<dict>)
``` ```
```python ```python
>>> BB = type('B', (), {'a': 'abcde', 'b': 12345})
>>> b = BB()
>>> Z = type('Z', (), {'a': 'abcde', 'b': 12345})
>>> z = Z()
``` ```
### MetaClass ### MetaClass

Loading…
Cancel
Save