#๐Ÿ”’ quick OOP question in super method (beginner)

22 messages ยท Page 1 of 1 (latest)

high jewel
#

I am not pasting the code here , i just want to ask that , here i have set entity behaviour to "foe" , that is the second argument , no how will i define an Enemy instance?

fleet apexBOT
#

@high jewel

Python help channel opened

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.

high jewel
#

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?

robust goblet
#
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

high jewel
#

Hope that makes sense

robust goblet
#

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

high jewel
#

No , if I create an instance in enemy class I NEED. 3 arguments

#

I think

odd moat
#
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.

robust goblet
#

No, you don't need 3 arguments

odd moat
#

And enemy1.behavior is still accessible and changeable later.

high jewel
#

I need to check brb

#

I need some sleep tbh

#

!close

fleet apexBOT
#
Python help channel closed

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.