r/learnpython • u/Darkwinggames • Jul 18 '21
How do I pass __init__ arguments to a subclass without repeating them?
class Modifier:
def __init__(self, value, name):
self.value = value
self.name = name
self.hidden = "yes"
self.generate_add_modifiers = "{produces}"
def generate_script(self):
script = f"""{{
#value = {self.value}
#name = {self.name}
hidden = {self.hidden}
generate_add_modifiers = {self.generate_add_modifiers}
}}"""
return script
class DerivedModifier(Modifier):
def __init__(self, is_positive):
self.is_positive = is_positiveThe above code is part of a game scripting language generator I'm working on. I've not dealt much with classes, but I assumed that arguments from a parents init method would get passed to it's childs init automatically.
However, pycharm is complaining about invalid arguments when I try to do the following:
m = DerivedModifier(name = Test, value = "50", is_positive = "True")Is this not the case? How can I achieve this without repeating myself?
75
u/Binary101010 Jul 18 '21
Your subclasses's __init__ only takes one parameter (aside from self) so any other parameter will cause an exception. Python won't automatically "pass through" other parameters; you need to do some of this work yourself.
The way to fix this is to have your subclass take all the parameters of its superclass as well, and then use super().__init__() to initialize everything else. This would look like
class DerivedModifier(Modifier):
def __init__(self, value, name, is_positive):
super().__init__(value, name)
self.is_positive = is_positive15
u/ManyInterests from fridge import beer Jul 19 '21
To extend on this: you can avoid needing to repeat the parameters from superclasses by using
*argsand**kwargsYou can do something like this:
class DerivedModifier(Modifier): def __init__(self, *args, is_positive, **kwargs): super().__init__(*args, **kwargs) self.is_positive = is_positiveThis means even if the superclass adds or removed arguments, your subclass doesn't have to change to accommodate that.
5
1
u/BobHogan Jul 19 '21
I highly recommend not doing this
def __init__(self, *args, is_positive, **kwargs):Its fine to use
*argsand**kwargsin the subclass__init__, but please do not put regular arguments in between them. That's just begging to have subtle bugs crop up
*argsand**kwargsshould always be placed at the end of your function definition, not interspersed with other parameters0
u/ManyInterests from fridge import beer Jul 19 '21
I highly recommend not doing this def init(self, *args, is_positive, **kwargs):
[...]
*args and **kwargs should always be placed at the end of your function definition, not interspersed with other parametersI would disagree strongly with this statement.
How exactly you define the signature depends, in part, how you want the API to look/work. But if you're talking about preventing 'subtle bugs' I'd argue it would be best to add any new arguments as keyword-only arguments (which is what happens as-written) and use
*argsat the beginning of the signature to preserve compatibility with the ordering of arguments from the superclass.Thus, to move from using
ModifiertoDerivedModifieryou need only provide the keyword argumentis_positive(if no default is defined).Modifier('name', 'value')would becomeDerivedModifier('name', 'value', is_positive=True)Also, it is a syntax error to place any arguments after
**kwargs, so what you suggest is not even possible. So, any new arguments MUST come before**kwargs.So, it seems the alternative you would necessarily be suggesting is to have this as the signature:
def __init__(is_positive, *args, **kwargs):This is a poor choice for a number of reasons:
You change the ordering of parameters, which becomes confusing to someone expecting a similar signature to the superclass. One would reasonably expect the positional arguments provided as
Modifier('name', 'value')to be the same respective arguments when provided toDerivedModifier, which would not be the case here and would almost certainly be a source of 'subtle bugs'.This breaks all
classmethods which use positional arguments.if a user wants to define
is_positiveusing a keyword argument (not positionally) then they must specify ALL arguments with keywords0
Jul 19 '21
[deleted]
1
u/ManyInterests from fridge import beer Jul 19 '21
Well, there's a tradeoff to consider here. In Python, you don't necessarily know in advance what class is being called when you use
super(). Superclasses can include your children's ancestors, for example.Additionally, the signature of your superclass may change. For example, the superclass may add new keyword arguments. Without
**kwargsyou would also need to update the signature of every subclass and classmethod. If you use*argsand**kwargsyou only have to update your base class(es).So in short, using
*argsand**kwargsmakes your code more flexible for subclassers and other future changes; your code won't have to change to accommodate for the changes that others make (which you often do not have the privilege of knowing in advance or may be in an entirely separate package).Using
*argsand**kwargsis functionally better for the reasons stated, but does introduce possible readability issues like you mention. The readability impacts might be offset somewhat by documentation and/or typing modules.Modern IDEs also know how to introspect this situation automatically. Suppose you have the following classes:
class MyBaseClass: def __init__(self, foo, bar): self.foo = foo self.bar = bar class MyDerivedClass(MyBaseClass): def __init__(self, *args, baz, **kwargs): super().__init__(*args, **kwargs) self.baz = bazIn a modern IDE, if you begin writing the constructor for
MyDerivedClass, the IDE will suggest all the appropriate arguments throughout the MRO.
24
u/Rawing7 Jul 18 '21
That's what varargs and argument unpacking are for:
class DerivedModifier(Modifier):
def __init__(self, is_positive, *args, **kwargs):
super().__init__(*args, **kwargs)
self.is_positive = is_positive20
Jul 18 '21
Trouble with that is that the constructor gets a non-useful signature.
12
u/Zalack Jul 18 '21
You can use @typing.overload to add the fully qualified signature.
1
Jul 19 '21
I didn't know about that! Cool!
Or
@functools.wrapsbehaves similarly, except you have to name the function or methodHowever, I believe neither of them actually work in this situation, because they still give you the wrong signature - without
is_positive.EDIT: OK, my statement is wrong about
@typing.overhead- you can specify any signature you like. But in this case I don't see the advantage over simply cutting and pasting the original signature and adding the new argument...?3
1
u/ManyInterests from fridge import beer Jul 19 '21
Most modern Python IDE's know how to step through
*args/**kwargsand super calls, too.As a simple example, take the following two classes:
class MyBaseClass: def __init__(self, foo, bar): self.foo = foo self.bar = bar class MyDerivedClass(MyBaseClass): def __init__(self, *args, baz, **kwargs): super().__init__(*args, **kwargs) self.baz = bazWhen writing the constructor for
MyDerivedClass, the IDE will suggest all the arguments automatically.You can also document your way out of it to say something like "accepts same arguments as MyBaseClass" -- which happens a lot in the official Python docs.
2
Jul 18 '21
There isn't really a way in all cases. For instance, inside __init__, if your arguments have default values, you don't know (without much effort) if those were indeed passed from outside, or just the defaults were used. It's also possible that your parent class will behave differently if it tries to figure out how many arguments were actually passed.
Another aspect of it: do you pass copies or references? Let's say, the semantics of your class are such that users expect it to modify the arguments given to it, for example, it expects an iterator. You cannot give the superclass the iterator, if it will also consume it... but there's really no way to clone an iterator in all cases (there's itertools.tee, but it means that you will be passing a wrapper object rather than the argument itself). Similarly, there are objects that cannot be cloned w/o semantically affecting them, most system resources are like that: file descriptors, processes, screen etc.
1
u/ReflectedImage Jul 18 '21
So if the arguments are exactly the same it does pass them through. But once you change the arguments you need to write a new init function on the subclass. That init function calls super().__init__(parent arguments here). Also don't put any arguments in super(), that was for the older Python 2.
1
u/Mayank1618 Jul 19 '21
If your init is exactly the same, dont redefine it in child class definition. if you need to pass additional commands, you either replace them all, or you need to initiate the init of parent class. use the super() function inside the init of child class. eg super().init( args). But you also need to add the arguments of parent class init to your subclass.
https://www.geeksforgeeks.org/python-call-parent-class-method/
0
Jul 19 '21
You can’t pass anything from a superclass to a subclass, only the other way. Your subclass has to pass the arguments up to the superclass method, using a call to super.
-1
u/kokoseij KokoseiJ Jul 18 '21
Please be sure to use super(DerivedModifier, self).__init__()
super() refers to the parent class of the instance. meaning, in some cases it could refer to wrong classes. By explicitly specifying the class and self object, super() will find the class that is the parent of the specified class.
6
2
u/seven0fx Jul 18 '21
sl. OT. How i use super() if i have 2 Parent Classes?
2
u/ParanoydAndroid Jul 18 '21
So, it's complicated. The short version is that it's often better to call the superclass explicitly instead of super.
If on the other hand you control the entire object hierarchy, you can use super but need to write your entire hierarchy to be compatible with cooperative multiple inheritance.
What this means is that every
__init__in the hierarchy must callsuper(). (Or whatever method you're trying to use super with, init isn't special)This is because super does not actually call only the superclass' implemention. It actually calls the next implementation of that method in the current class' method resolution order (MRO).
So if I have:
class A: ... class B: ... class C(A, B): ...Then class C has an MRO of [a, b].
If every class has a super call in init, then instantiating C will call C's init, which will call the next init in MRO order -- so, A. Then if A's init has a super call, that will cause the next init in the MRO to be called, which is B's.
If, for example, A didn't have a super call, then B's init will never be called from a super call in a method on C.
Hence, if you can't control the entire hierarchy (or aren't otherwise sure your parent classes are written to be compatible with cooperative multi inheritance) you should just call your superclass with an explicit reference.
1
1
u/seven0fx Jul 18 '21
My current approach would be like this.
Create my own Class with all Modules as Parent so that i can access all methods from these Classes.
from fritzconnection import FritzConnection from paho.mqtt.client import Client as MqttClient
class mqclient(MqttClient,FritzConnection): def init(self) -> None: # MQTT self.mq = super().init() self.mq = MqttClient() self.mq.connect(host='pi') self.mq.subscribe(topic="test/#") self.mq.on_message = self.on_message
# FritzBox self.fc = FritzConnection(address = 'fb') self.fc.call_action('') self.fc.reconnect() def on_message(self,client, userdata, msg): print(f'{msg.topic} {msg.payload.decode(encoding="utf-8")}') def run(self): try: self.mq.loop_forever() except KeyboardInterrupt: self.mq.loop_stop() exit()gg = mqclient() gg.run()
wrong approach or ok?
2
u/equitable_emu Jul 19 '21
That's a perfectly fine approach. It's a type of facade or aggregation approach.
https://en.wikipedia.org/wiki/Facade_pattern
But to more correctly implement it, you wouldn't inherit from both parent classes like you are now, you'd pass them in as parameters to the constructor.
from fritzconnection import FritzConnection from paho.mqtt.client import Client as MqttClient class mqclient(): def __init__(self, mqtt_client: MqttClient, fritz_connection: FritzConnection) -> None: # MQTT self.mq = mqtt_client self.mq.on_message = self.on_message # FritzBox self.fc = fritz_connection self.fc.call_action('') self.fc.reconnect() def on_message(self,client, userdata, msg): print(f'{msg.topic} {msg.payload.decode(encoding="utf-8")}') def run(self): try: self.mq.loop_forever() except KeyboardInterrupt: self.mq.loop_stop() exit() mqtt_client = MqttClient() mqtt_client.connect(host='pi') mqtt_client.subscribe(topic="test/#") gg = mqclient( mqtt_client=mqtt_client, fritz_connection=FritzConnection(address = 'fb')) gg.run()2
u/SwizzleTizzle Jul 19 '21
I'm trying to understand why you would implement something this way, rather than just subclassing MqttClient and overriding init and on_message
2
u/equitable_emu Jul 19 '21
Yeah, this is a pretty bad example because it doesn't really show off any of the facade capabilities.
I'm not too familiar with the FritzConnection API, so I don't even know if this makes sense with how that library is supposed to be used, but a better example might be something like this:
from fritzconnection import FritzConnection from paho.mqtt.client import Client as MqttClient class mqclient(): def __init__(self, mqtt_client: MqttClient, fritz_connection: FritzConnection) -> None: # MQTT self.mq = mqtt_client self.mq.on_message = self.on_message # FritzBox self.fc = fritz_connection # Expose the mqtt_client's connect method def connect(self, host: str): self.mq.connect(host=host) # Expose the mqtt_client's subscribe method def subscribe(self, topic: str): self.subscribe = mq.subscribe # Expose the mqtt_client's loop_forever method def loop_forever(self): self.mq.loop_forever() # Expose the mqtt_client's loop_stop method def loop_stop(self): self.mq.loop_stop() # Expose the fritz_connection's call_action method def call_action(self, action: str): self.fc.call_action(action) # Expose the fritze_client's reconnect method def reconnect(self): self.fc.reconnect() def on_message(self,client, userdata, msg): print(f'{msg.topic} {msg.payload.decode(encoding="utf-8")}') # Run until we get a keyboard interrupt, then return from this function # The return value is some type of exit status indicating why it stopped # (in this example, return 0 if a KeyboardInterrupt, -1 for any other exception ) def run() -> int: rtn_result = None try: self.loop_forever() except KeyboardInterrupt: rtn_result = 0 except Exception as e: print(f'Got exception: {e}') rtn_result = -1 finally: self.loop_stop() return rtn_result gg = mqclient( mqtt_client=MqttClient(), fritz_connection=FritzConnection(address = 'fb') ) # Now, we use both the MqttClient and FritzConnection exposed methods, without exposing everything gg.connect(host='pi') gg.subscribe(topic="test/#") gg.call_action('') gg.reconnect() # We move the control loop out of the client so the client itself doesn't exit the application, the calling function does rtn = gg.run() if rtn == 0: print("Execution stopped by KeyboardInterrupt") else: print("Execution stopped by Exception")This better illustrates the facade aspect, the mqclient doesn't have all the properties/methods of both MqttClient and FritzConnection, it only has the properties/methods from those that a user of the class would need.
1
1
u/WikiSummarizerBot Jul 19 '21
The facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code.
[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5
19
u/[deleted] Jul 18 '21
Another solution - use dataclasses, which will write the constructors for you.