Python - Manually wrapping a method (Specifically, am asking about line-profiler by robert kern)

Python - Manually wrapping a method (Specifically, am asking about line-profiler by robert kern)



Wrapping a function is no problem: How do I use line_profiler (from Robert Kern)?


from line_profiler import LineProfiler
import random

def do_stuff(numbers):
s = sum(numbers)
l = [numbers[i]/43 for i in range(len(numbers))]
m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()



However, what I can't seem to figure out it is applying this same technique to methods.



Let's say I attempt to use the same technique on a method:


class Foo:
def method(self):
return 1

obj = Foo()

lp = LineProfiler()
lp_wrapper = lp(method)
obj.lp_wrapper() # Causes an error since Foo does not have a method called "lp_wrapper"



What is the best way to fix this error? Thanks.






Have you tried lp_wrapper = lp(obj.method)

– schwobaseggl
Sep 7 '18 at 8:12


lp_wrapper = lp(obj.method)






schwobaseggl - I love you. This is it.

– MSam
Sep 8 '18 at 0:54






@Aaron In SO you should not add SOLVED or similar to indicate that you have solved your problem, just publish a response as you have done and mark it as correct. I recommend you check the tour :)

– eyllanesc
Sep 8 '18 at 0:59




1 Answer
1



The best answer comes from schwobaseggl: "Have you tried lp_wrapper = lp(obj.method)"


lp_wrapper = lp(obj.method)



It turns out that this is the way you wrap methods.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế