Dynamic method creation in python

I have a class that will have a number of external methods that will all call the same smaller set of internal methods. So something like:

obj.method_one(a, c) and
obj.method_two(a, c)

where obj.method_one calls obj._internal_method(a, c, y) and obj.method_two calls obj._internal_method(a, c, z).

They’re nearly identical but they have one argument at the end that differs on the internal call (which is a constant).

I could just copy and paste a few times but I was wondering if theres any way to dynamically create these external methods.

There are ways, but unless you have at least half a dozen, I’d suggest:

class myclass(object):
   def _internal_method(self,a,c,y):
       pass # add code here

   def method_one(self,a,c): self._internal_method(a,c,"method one")
   def method_two(self,a,c): self._internal_method(a,c,"another method")

The other option is to actually really create it dynamically. This can be confusing, because if the object is inspected, the methods aren’t there. It looks like this:

class test(object):
    def __getattr__(self,attrname):
        def _internal_method(a,c):
            print "You called %s  with %s and %s" % (attrname,a,c)
        if attrname in ["method_one","method_two"]:
            return _internal_method
        else:
            raise NameError

a=test()
a.method_one("first","second")

This makes use of the fact that a getting a property (like a method) that does not exist, the object’s __getattr__ is called, and the result is passed back. Note that we are passing back the FUNCTION _internal_method as a.method_one, not calling it at this stage; that is done after the call to getattr has returned. Think of the last line as being:

tmp=a.method
tmp("first","second")

The first of these calls __getattr__ which returns _internal_method (but does not call it), the latter calls _internal_method.

Since _internal_method is declared within __getattr__, it has the same scope as the call that created it, so self and attrname are accessible. Also, _internal_method is TRULY private, not just “private by convention”.

I will reiterate that this is confusing for the next person to maintain your code. For example, if you ask for help(a.method_one), you will get the help for _internal_method instead!

The length of this explanation, and your probable confusing is a very good reason not to use this unless you have to.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật