Created
February 5, 2021 16:50
-
-
Save adimiti/1add80090a32e87fb0d2fe1c3a87e1a7 to your computer and use it in GitHub Desktop.
[OOP] 10x RYO !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Thanks RYO for the hints | |
| class MyBase: | |
| name = 'Base' | |
| grazelist = [] | |
| def __init__(self, x=None): | |
| print 'Base:', x | |
| if x: | |
| self.x = x | |
| def graze(self, x): | |
| print 'Default Graze:', x | |
| print 'Grazelist', self.grazelist | |
| class MyClass1(MyBase): | |
| pass | |
| name = "WHISKY" | |
| grazelist = ['AA', 'BB'] | |
| class MyClass2(MyBase): | |
| pass | |
| name = "LIMA" | |
| grazelist = ['CC', 'DD'] | |
| class JustClass3(MyBase): | |
| pass | |
| name = "PAPA" | |
| import sys, inspect | |
| def print_classes(): | |
| for name, obj in inspect.getmembers(sys.modules[__name__]): | |
| if inspect.isclass(obj): | |
| print(obj) | |
| print sys.modules[__name__] | |
| clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass) | |
| clsdict = dict(clsmembers) | |
| print clsmembers | |
| a = MyClass1() | |
| print a.name, | |
| a.name = 'ZULU' | |
| print a.name | |
| b = MyClass1() | |
| print b.name | |
| z = [i[1].name for i in clsmembers if 'MyClass' in i[0]] | |
| print 'list =', z | |
| c = clsdict['MyClass2']() | |
| print 'instance:', c.name, c.__class__.__name__, isinstance(c, MyClass1), isinstance(c, MyClass2), isinstance(c, JustClass3), c.__class__ | |
| c.graze('YYY') | |
| d = clsdict['MyClass1']('Victor') | |
| print 'instance:', d.name, d.__class__.__name__, isinstance(d, MyClass1), isinstance(d, MyClass2), isinstance(d, JustClass3), d.__class__ | |
| d.graze('XXX') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment