Yes. The difference between the Goodbye
class and the Farewell
class is that Goodbye
has an instance member hello
, while the Farewell
class has a class member hello
. If you change the Hello
class that belongs to a Farewell
object, then all instances of Farewell
see the change, so you can do this:
a = Goodbye()
b = Goodbye()
a.hello.member = 1 # monkey-patching the member
b.hello.member = 2
print(a.hello.member) # prints "1"
print(b.hello.member) # prints "2"
f = Farewell()
g = Farewell()
f.hello.member = 1
g.hello.member = 2
print(f.hello.member) # prints "2"!
The reason this works is because, as you have defined it, instances of the Goodbye
class have their own instance of a Hello
object, while all instances of the Farewell
class share the same Hello
object. Check out the documentation to learn more!
Now whether or not one holds any advantage over the other is implementation-dependent. Class members can sometimes be confusing to users who might expect different instances of a class to retain their own state independent of other actions (i.e., mutable class members can break encapsulation.) However, it might be useful from an efficiency point of view to define members that are constant across all classes (making sure to somehow note this to the users, like hiding them behind underscore prefixes or clear documentation).