Created
March 7, 2019 17:43
-
-
Save dp-quant/4999cd747dda81946ef1d95c59f42c4a to your computer and use it in GitHub Desktop.
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
class Private: | |
__number = 1 | |
# def __add__(self, other): | |
def __say(self): | |
print('private say') | |
def say(self): | |
print('public say') | |
self.__say() | |
def _say(self): | |
print('protected say') | |
class Car: | |
VAN = 'van' | |
COUPE = 'coupe' | |
RACE = 'race' | |
@property | |
def __subtypes | |
return { | |
VAN: Van, | |
COUPE: Coupe, | |
RACE: Race | |
} | |
@classmethod | |
def factory(cls, sub_type, *args, **kwargs): | |
sub_car = self.__subtypes[sub_type] | |
if isinstance(sub_car, cls): | |
return self.__subtypes[sub_type](*args, **kwargs) | |
raise AssertionError | |
def drive(self): | |
raise NotImplementedError | |
class Van(Car): | |
pass | |
class Coupe(Car): | |
pass | |
class Race(Car): | |
pass | |
coupe = Car.factory(Car.COUPE) | |
van = Car.factory(Car.VAN) | |
class Number: | |
def __init__(self, n): | |
self.n = n | |
def __str__(self): | |
return str(self.n) | |
def __eq__(self, other): | |
return self.n == other.n | |
def __add__(self, other): | |
return self.__class__(self.n + other.n) | |
n1 = Number(1) | |
n2 = Number(1) | |
print(n1 == n2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment