+ 5
My abstract method is not working, why?
17 Antworten
+ 12
Abstract methods do not get implement in the abstract class. All subclasses must implement them. That is what abstract means.
+ 5
cat.info() will call the pet.info() method if it is not abstract. Abstract is meant for something like speak. They all do it in their own way. Not for some common like info.
+ 4
No remove the @abstractmethod from pet's info method and all three get it automatically. If you want to force each class to implement a speak method, that is when you use it.
+ 4
But the info method does not.
+ 3
Yes the class stays abstract
+ 3
Abstracting the class only means you can not create an instance of it. Everything non abstract in it could be used without issue by the subclasses. Any abstract method in it must be overrided in the subclasses meaning they have their own uniqueness.
+ 2
The whole purpose of having a share parent class is to hold everything they have in common.
+ 2
Lenoname missing colon
from abc import ABC, abstractmethod
class A(ABC) :
def __init__(self, name, lastname) : # colon missing
self.name = name
self.lastname = lastname
+ 1
John Wells how do i implement them then? The idea is to make cat bird hamster classes to inherit pet class and in the end call the info() method through other subclasses and not directly from pet class, by the way even if i delete info() method it wont work
+ 1
How do i make the pet class abstract without making the info() method abstract?
+ 1
you can't call an abstract method from the class you need a subclass
0
So i have to write info() method in all 3 subclasses?
0
John Wells Will the pet class then stay abstract if i remove the @abstractmethod?
0
The pet class must stay abstract
0
Ok how do i for example make this class abstract?:
class A :
def __init__(self, name, lastname)
self.name = name
self.lastname = lastname
0
from abc import ABC, abstractmethod
class A(ABC) :
def __init__(self, name, lastname)
self.name = name
self.lastname = lastname
Is this enough?
- 1
Anyone answer this question
How can this answer output
Print (x*10) 👈🙏