I'd like to see traits some day, with a syntax similar to this one:

trait Trait:
  def x(self):
    raise NotImplementedError
  def y(self):
    raise NotImplementedError

trait Anoher:
  def x(self):
    raise NotImplementedError
  def y(self):
    raise NotImplementedError

def foo(Trait(x)):
  x.x()

class Bar:
  def y(self):
    print("hello")
  impl Trait:
    def x(self):
      self.y()  # resolves to Bar.y
  impl Another:
    def x(self):
      raise ValueError

foo(Bar())  # prints 'hello'
Trait(Bar()).x()  # also prints 'hello'
Bar().x()  # AttributeError: ambiguous reference to trait method x

if the trait isn't used in the function definition you get the raw object, where name conflicts between traits (but not between traits and inherent methods) result in an error about name conflicts. otherwise you get a friendly wrapper.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MMTEZRNSPHB55IYCKJ2E2CXDRN7KYURC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to