My question is different than the title implies (I don't know how to summarize the question so I'm having a hard time googling).
I do not want a Union type. Union[A, B] says that the type can be either of type A, or of type B.
I need to the opposite. I want it to mean that it is both type A and B, which is possible in python because of mixins.
That is, I need to type hint a function such that I know the arguments passed will be of a class that has both A and B as parents, as my function uses methods from both mixins. A Union type hint allows passing something that has A without B which should not be allowed.
Example
from typing import Union
class A(object):
def a(self):
return True
class B(object):
def b(self):
return True
class C(A, B):
pass
def foo(d: Union[A,B]) -> bool: #need something other than Union!
print(d.a() and d.b())
I need d to be an A and a B. But currently it allows me to send things that are A without being B, and errors when it tries to call the non-existent function
>>> foo(A())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
AttributeError: 'A' object has no attribute 'b'
>>> foo(B())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
AttributeError: 'B' object has no attribute 'a'
>>> foo(C())
True
Further I'd like to note, that the type can't just be d: C
. This is because there are many classes that have A and B, and it would be a ridiculously long Union that would need to be maintained.
To return multiple values from a function in Python, return a tuple of values. As you may know, a tuple is a group of comma-separated values. You can create a tuple with or without parenthesis. To access/store the multiple values returned by a function, use tuple destructuring.
A function is not restricted to return a variable, it can return zero, one, two or more values. This is the default property of python to return multiple values/variables which is not available in many other programming languages like C++ or Java.
Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. The name: str syntax indicates the name argument should be of type str . The -> syntax indicates the greet() function will return a string.
Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.
You can use next OOP approach.
Create interface - it's abstract class in python, which can show methods, which implements concrete classes. Example:
from abc import ABC, abstractmethod
class MyAB(ABC):
@abstractmethod
def a(self):
pass
@abstractmethod
def b(self):
pass
class A(object):
def a(self):
return True
class B(object):
def b(self):
return True
class ConcreteClass(MyAB, A, B):
pass
def foo(d: MyAB):
print(d.a() and d.b())
c = ConcreteClass()
foo(c)
d
in function foo
can be able using two methods a
and b
. Thats all that you need.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With