2012-03-27 34 views
11

Tôi lập trình bằng Python, và tôi tự hỏi nếu tôi có thể kiểm tra nếu một hàm được gọi trong mã của tôiTìm hiểu Nếu một chức năng đã được gọi là

def example(): 
    pass 
example() 
#Pseudocode: 
if example.has_been_called: 
    print("foo bar") 

Làm thế nào tôi sẽ làm điều này?

+0

tôi đã viết một [đếm trang trí] (http: //code.activestate.com/recipes/577534-counting-decorator/?in=user-4173873) khi áp dụng sẽ cho bạn biết số lần một hàm được gọi. Bạn có thể điều chỉnh điều này theo nhu cầu của bạn nếu bạn muốn. –

+0

Bạn hy vọng sẽ làm gì với thông tin này? –

Trả lời

20

Nếu đó là bật đèn xanh cho các chức năng để biết tên riêng của mình, bạn có thể sử dụng một chức năng thuộc tính:

def example(): 
    example.has_been_called = True 
    pass 
example.has_been_called = False 


example() 

#Actual Code!: 
if example.has_been_called: 
    print("foo bar") 

Bạn cũng có thể sử dụng một trang trí để thiết lập các thuộc tính:

import functools 

def trackcalls(func): 
    @functools.wraps(func) 
    def wrapper(*args, **kwargs): 
     wrapper.has_been_called = True 
     return func(*args, **kwargs) 
    wrapper.has_been_called = False 
    return wrapper 

@trackcalls 
def example(): 
    pass 


example() 

#Actual Code!: 
if example.has_been_called: 
    print("foo bar") 
+0

Thật thú vị khi biết rằng hàm có thể nhận được một thuộc tính vì mọi thứ trong Python là một đối tượng. Các hàm là các đối tượng của lớp "hàm". Và bạn có thể gán một thuộc tính cho một cá thể bởi vì bạn không khai báo các biến trong Python, vì vậy bạn có thể gán chúng trong thời gian chạy. –

0

Dưới đây là một trang trí sẽ xem tất cả các chức năng của bạn, sử dụng colorama, để có được một đầu ra tốt đẹp.

thử: nhập khẩu coloramac trừ ImportError: lớp stdClass: pass def passer (* args, ** kwargs): pass colorama = stdClass() colorama.init = passer colorama.Fore = stdClass () colorama.Fore.RED = colorama.Fore.GREEN = ''

def check_for_use(show=False): 
    if show: 
     try: 
      check_for_use.functions 
     except AttributeError: 
      return 
     no_error = True 
     for function in check_for_use.functions.keys(): 
      if check_for_use.functions[function][0] is False: 
       print(colorama.Fore.RED + 'The function {!r} hasn\'t been called. Defined in "{}" '.format(function, check_for_use.functions[function][1].__code__.co_filename)) 
       no_error = False 
     if no_error: 
      print(colorama.Fore.GREEN + 'Great! All your checked function are being called!') 
     return check_for_use.functions 
    try: 
     check_for_use.functions 
    except AttributeError: 
     check_for_use.functions = {} 
     if colorama: 
      colorama.init(autoreset=True) 

    def add(function): 
     check_for_use.functions[function.__name__] = [False, function] 
     def func(*args, **kwargs): 
      check_for_use.functions[function.__name__] = [True, function] 
      function(*args, **kwargs) 
     return func 
    return add 

@check_for_use() 
def hello(): 
    print('Hello world!') 

@check_for_use() 
def bonjour(nb): 
    print('Bonjour tout le monde!') 


# hello(); bonjour(0) 

hello() 


check_for_use(True) # outputs the following 
Output:
Hello world! 
The function 'bonjour' hasn't been called. Defined in "path_to_file.py" 
Các vấn đề liên quan