## python修饰符的引用计次问题

ka-keung 发布于 2014/11/26 21:38

1    'xxxx' 'xxxxx'

1、首先我遇到的问题，为什么引用的次数不是递增则是直接显示累计的结果?

```#!/usr/bin/python
#--*encoding=utf8--*
import os
num=0

def hehe(w=1):
global num
num=sum+1
def xiushiqi(fun):
def test(*args):
os.chdir('/var/tmp')
print num,os.getcwd(),fun(*args)
return test
return xiushiqi

@hehe(num)
def ljq(x,y,z):
return x*y+z

@hehe(num)
def xx(x,y,z):
return x*y+z

@hehe(num)
def xwx(x,y,z):
return x*y+z

ljq(2,9,29)
xx(22,9,29)
xwx(8,9,29)

[root@test-A opt]# python c.py
3 /var/tmp 47
3 /var/tmp 227
3 /var/tmp 101```

1 /var/tmp 47
2 /var/tmp 227
3 /var/tmp 101

2、有没有一种比较好的方法实现我的需求？？？

0
```#!/usr/bin/python
#--*encoding=utf8--*
import os
num=0

def hehe(w=1):
global num
num=num+1
i=num
def xiushiqi(fun):
def test(*args):
print i,fun(*args)
return test
return xiushiqi

@hehe(num)
def ljq(x,y,z):
return x*y+z

@hehe(num)
def xx(x,y,z):
return x*y+z

@hehe(num)
def xwx(x,y,z):
return x*y+z

ljq(2,9,29)
xx(22,9,29)
xwx(8,9,29)```

`ljq(2,9,29)`

k

k

0

```# -*- coding:utf-8 -*-
num=0

def hehe(fun):
global num
num = num+1
i = num
print '->0', i, num, id(i), id(num)  # 1
def test(*args):
print '->1', i, num, id(i), id(num),  fun # 2
return test

@hehe
def ljq(x,y,z):
return x*y+z

@hehe
def xx(x,y,z):
return x*y+z

@hehe
def xwx(x,y,z):
return x*y+z#

print hehe(None) # 3  直接调用hehe
print '----------------4' #4

print ljq, ljq.__name__
print xx, xx.__name__
print xwx, xwx.__name__ # 5 打印xwx的函数名

ljq(2,9,29)
xx(22,9,29)
xwx(8,9,29)

```

k

k