M
M
Marina18022021-05-14 10:50:26
Python
Marina1802, 2021-05-14 10:50:26

How to make a python pattern?

Help me create a singleton pattern or decorator in this code please. How best to write it and what to 'attach' to?

class Pet:
  def __init__(self, name, colour):
    self.__name = name
    self.__colour = colour

  @property
  def colour(self):
    return self.__colour
    
  @colour.setter
  def colour(self, colour):
    if colour == 'grey':
      self.__colour = colour
    else:
      print("Такого кота не існує")
        
  @property
  def name(self):
    return self.__name
  def display_info(self):
    print("Ім’я:", self.__name, "Колір:", self.__colour)

         
marsik = Pet("Marsik", "grey")

marsik.colour = "blue"
marsik.colour = "grey"
marsik.display_info()

#Декоратори
def feeding(fn):
  def feed():
    print("Годувати кота")
    fn()
    print("Перестати годувати кота")
  return feed
@feeding
def feed_cat():
  print("Годую кота")
print(feed_cat())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
12rbah, 2021-05-14
@12rbah

Here is an example of how to make such a decorator pattern with your code, I advise you to figure it out yourself

A
alexbprofit, 2021-05-14
@alexbprofit

returnfeed()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question