Python Forum
"unexpected keyword arg" when initializing my subclasses
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"unexpected keyword arg" when initializing my subclasses
#4
(Nov-25-2022, 07:12 PM)Phaze90 Wrote: why I shouldn't inherit from that Base-Class?
Metaclasses are for metaprogramming. You don't need that for your first class, so remove the metaclass.

Private attributes with double underscores are more an annoyance than anything else. In Python there is no compiler to enforce more or less privacy on members, so privacy is usually a gentlemen's agreement between the programmer and his/her colleagues. To indicate that an attribute is not in the object's public interface, use a single underscore.

You could write the code this way for example

class MyBaseClass:

    def __init__(self,
                 material=None,
                 origin=None,
                 manufacturer=None,
                 ) -> None:

        self._material = material
        self._origin = origin
        self._manufacturer = manufacturer

class MyClass1(MyBaseClass):
    def __init__(self,
                 name=None,
                 length=None,
                 height=None,
                 color=None,
                 **kwargs,
                 ) -> None:

        super().__init__(**kwargs)

        self._name = name
        self._length = length
        self._height = height
        self._color = color


class MyClass2(MyBaseClass):
    def __init__(self,
                 name=None,
                 vol=None,
                 weight=None,
                 dense=None,
                 **kwargs,
                 ) -> None:

        super().__init__(**kwargs)

        self._name = name
        self._vol = vol
        self._weight = weight
        self._dense = dense

k1 = MyClass1(name='name',
              length='leng',
              height='heig',
              color='colo',
              material='mate',
              origin='orig',
              manufacturer='manu',
            )
print(k1)
print(vars(k1))
Output:
<__main__.MyClass1 object at 0x7fd271dd3ee0> {'_material': 'mate', '_origin': 'orig', '_manufacturer': 'manu', '_name': 'name', '_length': 'leng', '_height': 'heig', '_color': 'colo'}
Phaze90 likes this post
Reply


Messages In This Thread
RE: "unexpected keyword arg" when initializing my subclasses - by Gribouillis - Nov-25-2022, 07:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Django "Unexpected Keyword Argument 'min_value'" AstralWeeks 0 209 Mar-27-2024, 04:56 AM
Last Post: AstralWeeks
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 6,949 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  Find a specific keyword after another keyword and change the output sgtmcc 5 808 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  which design / pattern when building classes and subclasses Phaze90 2 1,123 Nov-19-2022, 08:42 AM
Last Post: Gribouillis
  Initializing a PRINTDLG structure-how to hammer 4 1,488 Jun-08-2022, 07:07 PM
Last Post: jefsummers
  Syntax when initializing empty lists Mark17 2 1,357 Jun-02-2022, 04:09 PM
Last Post: Mark17
  Initializing, reading and updating a large JSON file medatib531 0 1,769 Mar-10-2022, 07:58 PM
Last Post: medatib531
  TypeError: __init__() got an unexpected keyword argument 'value' Anldra12 7 22,628 May-11-2021, 06:35 PM
Last Post: deanhystad
  User Subclasses holyghost 6 3,177 Mar-17-2021, 12:33 PM
Last Post: buran
  TypeError: coerce_kw_type() got an unexpected keyword argument 'dest' juniii 1 2,427 Apr-24-2020, 01:53 PM
Last Post: Jeff900

Forum Jump:

User Panel Messages

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