#๐ quick OOP question in super method (beginner)
22 messages ยท Page 1 of 1 (latest)
@high jewel
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
because i will need to set values of all three arguments to define an instance
how can i avoid setting the 2nd argument (its value should be fix , i.e "foe")
or is there any other way to do this ig?
class E:
def __init__(self, a, b=2):
self.a=a
self.b=b
class D(E):
def __init__(self, a, c, b=None):
super().__init__(a, b)
self.c = c
test = D(1, 3)
print(test.a, test.b, test.c)
you have unused "b" parameter in your Enemy class
so move the argument to the last place in init method of an Entity class and give it a default value
But default value of entity behaviour attribute should be foe for enemy class only
Hope that makes sense
then, make a default of "foe"
so the init would look:
...(self, a, b, c="foe")
then:
super().__init__(a, c)
Then you can create a class: ("Golem", 78)
and the third parameter is not given, so by default it is "foe" which will be passed to the Entity class
class Entity:
def __init__(self, name, behavior):
self.name = name
self.behavior = behavior
class Enemy(Entity):
def __init__(self, name, type_):
super().__init__(name=name, behavior="foe")
self.type_ = type_
enemy1 = Enemy("Golem", 78)
Nothing wrong with defaulting the arg without actually naming it in the init for Enemy, if that's what you want.
No, you don't need 3 arguments
And enemy1.behavior is still accessible and changeable later.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.