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
23.8k5 gold badges70 silver badges127 bronze badges
asked Jan 8, 2015 at 8:20
1
  • 2
    For a pretty extensive discussion of this and related topics (e.g. adding a method to an object instance), see this SO post: stackoverflow.com/questions/972/… Commented Jan 8, 2015 at 8:32

1 Answer 1

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".

answered Jan 8, 2015 at 8:30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.