interface

How to create a stub Python package for PyCharm?

How to create a stub Python package for PyCharm? Question: Let’s say we have a builtin module car. We have no autocomplete in PyCharm for it. It has interface: def getState(id, optional_id = None): pass def log(value): pass So, end usage would be: import car car.getState(5) car.log(‘Test’) How to create a package and join it …

Total answers: 1

mypy doesn't understand class and interface are the same

mypy doesn't understand class and interface are the same Question: from abc import ABC, abstractmethod class IState(ABC): """Interface para o padrĂ£o de projeto State.""" @abstractmethod def sucesso_ao_pagar(self) -> None: pass @abstractmethod def despachar_pedido(self) -> None: pass @abstractmethod def cancelar_pedido(self) -> None: pass class Pedido: """Classe que representa um pedido.""" def __init__(self) -> None: self.estado_atual = …

Total answers: 1

I've a probleme with tkinter, with the pady

I've a probleme with tkinter, with the pady Question: Here is my code: (sorry it’s in french) from tkinter import * # creer la fenetre window = Tk() window.title("Interface projet-NSI vacances noel") window.geometry("1080×720") window.iconbitmap("ricardo.ico") window.config(background=’#4065A4′) # creer la frame principale frame = Frame(window, background="#4065A4") # creer des sous boites top_left_frame = Frame(window, background="#4065A4") # creer …

Total answers: 1

What to use in replacement of an interface/protocol in python

What to use in replacement of an interface/protocol in python Question: I am making a chess game and wanted to make a standard piece interface/protocol. Python does not have those in the language, so what am I supposed to use? I read a bit about factories, but I’m not sure how they would help. Thanks …

Total answers: 8

Implementing interfaces in Python?

Implementing interfaces in Python? Question: To my knowledge, there is no explicit built-in feature of Python that allows to implement interfaces. Consider the following situation: I have a project with multiple classes. And some of them use a specific method. This method is not functionally or logically coherent with any of them, and technically should …

Total answers: 4

What is the purpose of checking self.__class__?

What is the purpose of checking self.__class__? Question: What is the purpose of checking self.__class__ ? I’ve found some code that creates an abstract interface class and then checks whether its self.__class__ is itself, e.g. class abstract1 (object): def __init__(self): if self.__class__ == abstract1: raise NotImplementedError(“Interfaces can’t be instantiated”) What is the purpose of that? …

Total answers: 5

Why can `__subclasshook__` be monkeypatched onto the metaclass but `__instancecheck__` cannot?

Why can `__subclasshook__` be monkeypatched onto the metaclass but `__instancecheck__` cannot? Question: Here is a toy example of trying to create a decorator that allows declaration of attribute names which should be required parts of “interface checking” along the standard __subclasshook__ and __instancecheck__ patterns. It seems to work as expected when I decorate the Foo …

Total answers: 1

How to extend a class in python?

How to extend a class in python? Question: In python how can you extend a class? For example if I have color.py class Color: def __init__(self, color): self.color = color def getcolor(self): return self.color color_extended.py import Color class Color: def getcolor(self): return self.color + " extended!" But this doesn’t work… I expect that if I …

Total answers: 4

Java abstract/interface design in Python

Java abstract/interface design in Python Question: I have a number of classes which all share the same methods, only with different implementations. In Java, it would make sense to have each of these classes implement an interface or extend an abstract class. Does Python have anything similar to this, or should I be taking an …

Total answers: 5