Atrybut w metodzie init a atrybut w klasie

0

Witam.

Robiłem sobie ćwiczenia z książki "PYTHON INSTRUKCJE DLA PROGRAMISTY" i mam problem nie rozumiem pewnej rzeczy

class Restaurant():
  def __init__(self, restaurant_name, cuisine_type):
    self.restaurant_name = restaurant_name
    self.cuisine_type = cuisine_type
    self.number_served = 0

  def describe_restaurant(self):
    print(f"Restauracja {self.restaurant_name} serwuje kuchnie {self.cuisine_type}. Restauracja obsłużyła dziś {self.number_served} klientów.")

  def open_restaurant(self):
    print(f"Restauracja {self.restaurant_name} jest otwierana od 11 - 23")

  def set_number_served(self, client_base):
    self.number_served = client_base

  def increment_number_served(self, client):
    self.number_served += client


class IceCreamStand(Restaurant):
  def __init__(self, restaurant_name, cuisine_type):
    super().__init__(restaurant_name, cuisine_type)
    self.flavors = ['śmietankowe', 'truskawkowe', 'orzechowe']

  def ice_cream_flavors(self):
    print('Dziś są dostępne w naszej lodziarni smaki: ')
    for self.flavor in self.flavors:
      print(f" - {self.flavor}")


new_restaurant = Restaurant('KFC', 'Fast Food')
ice_cream = IceCreamStand('Algida', 'Lody')
ice_cream.describe_restaurant()
ice_cream.ice_cream_flavors()

Klasa IceCreamStand dziedziczy po klasie Restaurant. W klasie IceCreamStand mam atrybut flavors zawierający smaki lodów. Po stworzeniu instancji i wywołaniu metody ice_cream_flavors() kod działa poprawnie. Zrobiłem też podobny kod i z nim mam problem.

class User():

  def __init__(self, first_name, last_name, login, email):
    self.first_name = first_name
    self.last_name = last_name
    self.login = login
    self.email = email
    self.login_attempts = 0

  def describe_user(self):
    print(f"Witaj {self.first_name} {self.last_name}. Twój login to {self.login} a adres e-mail {self.email}")

  def greet_user(self):
    print(f"Witaj {self.login} co mogę dla Ciebie zrobić?")
    print(f"Użytkownik o loginie {self.login} logował się już {self.login_attempts} razy.")

  def increment_login_attempts(self):
    self.login_attempts += 1

  def reset_login_attempts(self):
    self.login_attempts = 0

class Admin(User):

  
  def __init(self, first_name, last_name, login, email):
    super().__init__(first_name, last_name, login, email)
    self.privileges = ['może dodać post', 'może usunąć post', 'może zbanować użytkownika']

  def show_privileges(self):
    print('Uprawnienia administratora: ')
    for self.privilege in self.privileges:
      print(f"- {self.privilege}")

new_user02 = Admin("Marian", 'XXX', 'ManiekAdmin', '[email protected]')
new_user02.show_privileges()

Chodzi o to, że przy wywołaniu metody show_privileges() dostaję błąd AttributeError: 'Admin' object has no attribute 'privileges'. Kod działa gdy atrybut privileges wyciągne z metody init i dam go jako zmienna klasy w ten sposób

class Admin(User):

  def __init(self, first_name, last_name, login, email):
    super().__init__(first_name, last_name, login, email)

privileges = ['może dodać post', 'może usunąć post', 'może zbanować użytkownika']

Nie rozumiem, jak to działa i jak to się poprawnie powinno robić. Może mi to ktoś łopatologicznie wytłumaczyć?

1

Tutaj Masz literówkę:

def __init(self, first_name, last_name, login, email):

Powinno być:

def __init__(self, first_name, last_name, login, email):
0

@lion137: Faktycznie nie zauważyłem tego, dziękuje wszytko gra. A jaka jest różnica między dodaniem atrybuty to metody init a dodaniem do klasy?

1 użytkowników online, w tym zalogowanych: 0, gości: 1