0

I need to accept emails via SMTP, for this decided to use SMTPD Lib in Python. There is a class SMTPChannel - is it possible to add a method to this class? I rather need not extend it, but do something that my method would be there on load...

MB-F
  • 22,770
  • 4
  • 61
  • 116
user1692333
  • 2,461
  • 5
  • 32
  • 64
  • 2
    For a pretty extensive discussion of this and related topics (e.g. adding a method to an object instance), see this SO post: http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object – zehnpaard Jan 08 '15 at 08:32

1 Answers1

4

You can dynamically add members to anything at runtime, including methods. You just need to define the method as a function separately, and then augment the type with the method:

def someMethod (self):
    # do something with self

SMTPChannel.someMethod = someMethod

Afterwards, all objects of type SMTPChannel will have access to that method.

Note that doing this will not work with the name mangling Python does for members starting with two underscores. So you can’t really do anything, you can’t do with the SMTPChannel object from “outside”.

poke
  • 369,085
  • 72
  • 557
  • 602