Python magic

Каблуков Станислав

PyNSK #10

 

Питон дескрипторы

Поиграем немного в Pokemon'ов

__init__, __new___

class Singleton(object):
    def __new__(cls):
        if not hasattr(cls, 'instance'):
            cls.instance = super(Singleton, cls).__new__(cls)
        return cls.instance

__metaclass__

from abc import ABCMeta, abstractmethod


class Pokemon(metaclass=ABCMeta):
    __metaclass__ = ABCMeta  # in python 2

    @abstractmethod
    def name(self):
        pass

    @abstractmethod
    def element_type(self):
        pass

__metaclass__

from pokemon.pokemon import Pokemon
from pokemon_type_attack.type_attack import TypeAttack


class Eevee(Pokemon):

    def __init__(self, рост, вес):
        self.рост = рост
        self.вес = вес
        self.attack_type = TypeAttack.normal

    def name(self):
        print("Eevee")

    def element_type(self):
        print("Normal")

    def damage_per_second(self, enemy_type: TypeAttack):

        if self.attack_type == enemy_type.normal:
            return 1
        elif self.attack_type == enemy_type.air:
            return 20
        else:
            return 10

class EvolvedEevee(Eevee):

    def __new__(cls, *args, **kwargs):

        list_type_pokemon_for_random_selection = [poke_type for poke_type in TypePokemon]

        type_pokemon_for_evolution = random.choice(list_type_pokemon_for_random_selection)

        if type_pokemon_for_evolution == TypePokemon.electric:
            cls.name = name_electric_pokemon
            cls.element_type = lambda self: print("electric")
            cls.attack_type = TypeAttack.electric

        elif type_pokemon_for_evolution == TypePokemon.water:
            cls.name = name_water_pokemon
            cls.element_type = lambda self: print("water")
            cls.attack_type = TypeAttack.water

        return super().__new__(cls, *args)

    def __init__(self, sound, рост, вес):
        super().__init__(рост, вес)
        self.sound = sound

    def play_sound(self):
        print(self.sound)
def __new__(cls, *args, **kwargs):

    list_type_pokemon_for_random_selection = \
        [poke_type for poke_type in TypePokemon]

    type_pokemon_for_evolution = \
        random.choice(list_type_pokemon_for_random_selection)

    if type_pokemon_for_evolution == TypePokemon.electric:
        cls.name = name_electric_pokemon
        cls.element_type = lambda self: print("electric")
        cls.attack_type = TypeAttack.electric

    elif type_pokemon_for_evolution == TypePokemon.water:
        cls.name = name_water_pokemon
        cls.element_type = lambda self: print("water")
        cls.attack_type = TypeAttack.water

    return super().__new__(cls, *args)


class EvolvedEevee(Eevee):

    def __new__(cls, *args, **kwargs):

        logic ...

        return super().__new__(cls, *args)

    def __init__(self, sound, рост, вес):
        super().__init__(рост, вес)
        self.sound = sound

    def play_sound(self):
        print(self.sound)
import random
from eevee.eevee import Eevee
from pokemon_type_attack.type_attack import TypePokemon, TypeAttack


class EvolvedEevee2(Eevee):
    def __init__(self, sound, рост, вес):
        super().__init__(рост, вес)

        list_type_pokemon_for_random_selection = \
            [poke_type for poke_type in TypePokemon]
        type_pokemon_for_evolution = \
            random.choice(list_type_pokemon_for_random_selection)

        self.sound = sound
        self.pokemon_type = type_pokemon_for_evolution

        if self.pokemon_type == TypePokemon.water:
            self.attack_type = TypeAttack.water
        elif self.pokemon_type == TypePokemon.electric:
            self.attack_type = TypeAttack.electric

    def name(self):
        if self.pokemon_type == TypePokemon.water:
            return "Vaporeon"
        elif self.pokemon_type == TypePokemon.electric:
            return "Jolteon"

    def element_type(self):
        return self.pokemon_type.value

    def play_sound(self):
        print(self.sound)
class EvolvedEeveeWithMeta(Eevee, metaclass=TypePokemon):

    def __init__(self, sound, рост, вес):
        super().__init__(рост, вес)
        self.sound = sound

    def play_sound(self):
        print(self.sound)







class EvolvedEeveeWithMeta(Eevee, metaclass=get_random_pokemon_type()):

    def __init__(self, sound, рост, вес):
        super().__init__(рост, вес)
        self.sound = sound

    def play_sound(self):
        print(self.sound)
__prepare__ 

Фабрика Класса

 

__slots__

__del__

__str__, __repr__, __format__

__bytes__

__lt__,__le__, __eq__, __ne__, __gt__, __ge__

__hash__

__call__

Python Magic

By Станислав