Is there a way to share def's between different classes? - python

I have an application with quite a number of classes. The classes have a number of functions as per def's that are common and obviously some that are not.
Is there a way to define the functions that have common function in one place and be available to a number of classes, or do I have to have at least the def's in every class?

Start by defining the func, which is defined outside.
Then to refer that function in different classes, use the same func inside the classes
def func():
print('func')
class B:
def funcB(self):
print('funcB')
def func(self):
func()
class C:
def funcC(self):
print('funcC')
def func(self):
func()
These can be called now as follows.
b = B()
b.func()
#funcA
b.funcB()
#funcB
c = C()
c.func()
#funcA
c.funcC()
#funcC

As pointed out by Amadan in the comments, class inheritance is a good and consistent way of sharing class objects. Below is an example:
class animals:
def __init__(self):
pass
def has_legs(self, type):
if type == "snake":
print(False)
else:
print(True)
class dog(animals):
def __init__(self):
# This is where the magic happens
animals.__init__(self)
pass
def dog_has_legs(self):
self.has_legs("dog")
bofur = dog()
bofur.dog_has_legs()
bofur.has_legs("snake")
Result:
>>> bofur.dog_has_legs()
True
>>> bofur.has_legs("snake")
False
As you can see, the class dog inherits from animals, and so it can call functions and other objects from the animals class as if they belonged to the dog class.

Related

Inherit multiple methods with same name from multiple classes

I have two parent classes that share method names. I'd like to subclass them and reassign their methods under different names to the subclass. This is made complicated because these methods use other methods with shared names as well. Contrived but minimal example:
class Foo1():
def __init__(self):
super().__init__()
def print(self):
print(self.get())
def get(self):
return 1
class Foo2():
def __init__(self):
super().__init__()
def print(self):
print(self.get())
def get(self):
return 2
class Foo(Foo1, Foo2):
def __init__(self):
super().__init__()
self.print1 = super().print
self.print2 = super(Foo1, self).print
foo = Foo()
foo.print1()
>>> 1
foo.print2()
>>> 1
In the example above, foo.print1() prints 1 as expected, but foo.print2() prints 1 instead of 2. I would like to structure Foo such that it calls Foo2's print and get methods and thus prints 2.
I understand this is because the name "self" actually points to Foo(), and so the methods "print" and "get" are pointing to Foo1's methods due to Foo1 being first in the MRO.
For my use case, it doesn't even make sense for Foo() to have "print" and "get" methods, only "print1" and "print2". This makes me suspect that I'm going about it all wrong. Is there a better way to construct a subclass that correctly inherits these methods from Foo1 and Foo2?
Have your class wrap Foo1 and Foo2 instances rather than using multiple inheritance.
class Foo:
def __init__(self):
foo1 = Foo1()
foo2 = Foo2()
self.print1 = foo1.print
self.print2 = foo2.print
self.get1 = foo1.get
self.get2 = foo2.get
One option is to use composition, where your new class instance is just a wrapper around two instances of the original classes. Your methods will delegate to methods of the appropriate instances.
class Foo:
def __init__(self):
self.foo1 = Foo1()
self.foo2 = Foo2()
def print1(self):
return self.foo1.print()
def print2(self):
return self.foo2.print()
# etc
You might also be able to perform the composition at the class level, rather than the instance level. This will only work if the function signatures of Foo1 and Foo2 methods are compatible with the desired signature for the Foo methods.
class Foo:
print1 = Foo1.print
print2 = Foo2.print
get1 = Foo1.get
get2 = Foo2.get
In either case, you'll have to define Foo.__init__ to accept any necessary arguments for Foo1.__init__ and Foo2.__init__, and ensure they are passed to the correct one.

Dynamically adopt the methods of an instance of another class

I have a case, where I have an instance of a class in python which holds instances of other classes. For my use case, I would like a way to use the methods of the "inner" classes from the outer class without referencing the attribute holding the inner class.
I have made a simplistic example here:
class A:
def __init__(self):
pass
def say_hi(self):
print("Hi")
def say_goodbye(self):
print("Goodbye")
class C:
def __init__(self, other_instance):
self.other_instance= other_instance
def say_good_night(self):
print("Good night")
my_a = A()
my_c = C(other_instance=my_a)
# How to make this possible:
my_c.say_hi()
# Instead of
my_c.other_instance.say_hi()
Class inheritance is not possible, as the object passed to C may be an instance of a range of classes. Is this possible in Python?
I think this is the simplest solution although it is possible with metaprogramming.
class A:
def __init__(self):
pass
def say_hi(self):
print("Hi")
def say_goodbye(self):
print("Goodbye")
class C:
def __init__(self, other_class):
self.other_class = other_class
C._add_methods(other_class)
def say_good_night(self):
print("Good night")
#classmethod
def _add_methods(cls, obj):
type_ = type(obj)
for k, v in type_.__dict__.items():
if not k.startswith('__'):
setattr(cls, k, v)
my_a = A()
my_c = C(other_class=my_a)
my_c.say_hi()
output :
Hi
First we get the type of passed instance, then we iterate through it's attribute (because methods are attributes of the class not the instance).
If self.other_class is only needed for this purpose, you can omit it as well.
So, because you have done:
my_a = A() and my_c = C(other_class=my_a).
my_c.other_class is the same as my_a asthey point to the same location in memory.
Therefore, as you can do my_a.say_hi() you could also do my_c.other_class.say_hi().
Also, just a note, as you are calling A() before you store it into other_classes, I would probably rename the variable other_classes to class_instances.
Personally, I think that would make more sense, as each of those classes would have already been instantiated.

pyhon3 dynamically create methods from parent class

Let's say I have a class defined like this:
classA():
def do_one():
print("one")
def do_two():
print("two")
def some_other():
print("other")
I want to create a derived class and automatically define every method from the parent class that starts with do
I tried this:
class B(A):
pass
for m in dir(A):
if m[0:3] == "do_":
def dm(self):
print("somebething before")
getattr(super(),m)()
dm.__name__ = m
setattr(B,m,dm)
But i'm getting this error: RuntimeError: super(): __class__ cell not found
Also is there a non-so-hacky/pytonic way of achieving this?
You cannot use super() without arguments outside a class definition. In order to perform lookups super() needs two arguments. Additionally, the definition of dm needs to be wrapped in another function. Otherwise, when it is executed the value of m will be taken from the global scope.
Putting this together, the following should work:
class A():
def do_one(self):
print("one")
def do_two(self):
print("two")
def some_other(self):
print("other")
class B(A):
pass
for m in dir(B):
if m[0:3] == "do_":
def redef(m):
def dm(self):
print("something before")
getattr(super(B, self), m)()
return dm
setattr(B, m, redef(m))
If you run:
x = B()
x.do_one()
x.do_two()
x.some_other()
It will give:
something before
one
something before
two
other

Conditional Inheritance based on arguments in Python

Being new to OOP, I wanted to know if there is any way of inheriting one of multiple classes based on how the child class is called in Python. The reason I am trying to do this is because I have multiple methods with the same name but in three parent classes which have different functionality. The corresponding class will have to be inherited based on certain conditions at the time of object creation.
For example, I tried to make Class C inherit A or B based on whether any arguments were passed at the time of instantiating, but in vain. Can anyone suggest a better way to do this?
class A:
def __init__(self,a):
self.num = a
def print_output(self):
print('Class A is the parent class, the number is 7',self.num)
class B:
def __init__(self):
self.digits=[]
def print_output(self):
print('Class B is the parent class, no number given')
class C(A if kwargs else B):
def __init__(self,**kwargs):
if kwargs:
super().__init__(kwargs['a'])
else:
super().__init__()
temp1 = C(a=7)
temp2 = C()
temp1.print_output()
temp2.print_output()
The required output would be 'Class A is the parent class, the number is 7' followed by 'Class B is the parent class, no number given'.
Thanks!
Whether you're just starting out with OOP or have been doing it for a while, I would suggest you get a good book on design patterns. A classic is Design Patterns by Gamma. Helm. Johnson and Vlissides.
Instead of using inheritance, you can use composition with delegation. For example:
class A:
def do_something(self):
# some implementation
class B:
def do_something(self):
# some implementation
class C:
def __init__(self, use_A):
# assign an instance of A or B depending on whether argument use_A is True
self.instance = A() if use_A else B()
def do_something(self):
# delegate to A or B instance:
self.instance.do_something()
Update
In response to a comment made by Lev Barenboim, the following demonstrates how you can make composition with delegation appear to be more like regular inheritance so that if class C has has assigned an instance of class A, for example, to self.instance, then attributes of A such as x can be accessed internally as self.x as well as self.instance.x (assuming class C does not define attribute x itself) and likewise if you create an instance of C named c, you can refer to that attribute as c.x as if class C had inherited from class A.
The basis for doing this lies with builtin methods __getattr__ and __getattribute__. __getattr__ can be defined on a class and will be called whenever an attribute is referenced but not defined. __getattribute__ can be called on an object to retrieve an attribute by name.
Note that in the following example, class C no longer even has to define method do_something if all it does is delegate to self.instance:
class A:
def __init__(self, x):
self.x = x
def do_something(self):
print('I am A')
class B:
def __init__(self, x):
self.x = x
def do_something(self):
print('I am B')
class C:
def __init__(self, use_A, x):
# assign an instance of A or B depending on whether argument use_A is True
self.instance = A(x) if use_A else B(x)
# called when an attribute is not found:
def __getattr__(self, name):
# assume it is implemented by self.instance
return self.instance.__getattribute__(name)
# something unique to class C:
def foo(self):
print ('foo called: x =', self.x)
c = C(True, 7)
print(c.x)
c.foo()
c.do_something()
# This will throw an Exception:
print(c.y)
Prints:
7
foo called: x = 7
I am A
Traceback (most recent call last):
File "C:\Ron\test\test.py", line 34, in <module>
print(c.y)
File "C:\Ron\test\test.py", line 23, in __getattr__
return self.instance.__getattribute__(name)
AttributeError: 'A' object has no attribute 'y'
I don't think you can pass values to the condition of the class from inside itself.
Rather, you can define a factory method like this :
class A:
def sayClass(self):
print("Class A")
class B:
def sayClass(self):
print("Class B")
def make_C_from_A_or_B(make_A):
class C(A if make_A else B):
def sayClass(self):
super().sayClass()
print("Class C")
return C()
make_C_from_A_or_B(True).sayClass()
which output :
Class A
Class C
Note: You can find information about the factory pattern with an example I found good enough on this article (about a parser factory)

Naming conventions for class method vs instance method

I have two methods, one for the individual Instance, and one for every Instance in that class:
class MasterMatches(models.Model):
#classmethod
def update_url_if_any_matches_has_one(cls):
# apply to all instances, call instance method.
def update_url_if_any_matches_has_one(self):
# do something
Should I name these the same? Or, what is a good naming convention here?
The question of using the same names can be clarified by understanding how decorators work.
#dec
def foo(x):
print(x)
translates to
def foo(x):
print(x)
foo = dec(foo)
In your example the decorator syntax can be expanded to
class MasterMatches(models.Model):
def update_url_if_any_matches_has_one(cls):
# apply to all instances, call instance method.
update_url_if_any_matches_has_one = classmethod(update_url_if_any_matches_has_one)
def update_url_if_any_matches_has_one(self):
# do something
The former implementation of update_url_if_any_matches_has_one will be overwritten by the latter.
Usually use self declaration style. #classmethod use only if method not works with class instance fields.
Function decorated as #classmethod takes the first argument is the class type, while normal method takes instance of object.
class A:
#classmethod
def a(cls):
print(cls)
def b(self):
print(self)
a = A()
a.a()
a.b()
# Output:
# <class '__main__.A'>
# <__main__.A object at 0x03FC5DF0>
It can be useful if you have a static class fields. The to access therm you don't need explicitly specify the class name. But you don't get access to instance fields. Example:
class A:
field = 1
#classmethod
def a(cls):
print(cls.field)
def b(self):
self.field = 2
print(self.field, A.field)
a = A()
a.a()
a.b()
# Outputs:
# 1
# 2 1

Categories

Resources