Python Forum
[Python Core] Keyword for direct passthrough of **kwargs to super().__init__
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python Core] Keyword for direct passthrough of **kwargs to super().__init__
#2
well, *args is catch all for positional arguments, **kwargs - for keyword (named) argument, so there is no way to have one more catch-all-pass-trough. You would do something like
class Animal:
    def __init__(self, **kwargs):
        self.species = kwargs.get('species', None)
class Mouse(Animal):
    def __init__(self, **kwargs):
        self.name = kwargs.get('name', None)
        super().__init__(**kwargs)

mouse = Mouse(species="Mouse", name='Mikey Mouse')
print(mouse.species)
print(mouse.name)
or if you want to have some explicit mentioned arguments
class Animal:
    def __init__(self, species, **kwargs):
        self.species = species
class Mouse(Animal):
    def __init__(self, name, **kwargs):
        self.name = name
        super().__init__(**kwargs)

mouse = Mouse(species="Mouse", name='Mikey Mouse')
print(mouse.species)
print(mouse.name)
in both cases output is
Mouse
Mikey Mouse

the second example assume the parent class has no argument name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: [Python Core] Keyword for direct passthrough of **kwargs to super().__init__ - by buran - May-25-2018, 05:17 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020