小编nar*_*ren的帖子

python:当class属性,实例属性和方法都具有相同的名称时会发生什么?

当名称相同时,python如何区分类属性,实例属性和方法?

class Exam(object):

    test = "class var"

    def __init__(self, n):
        self.test = n

    def test(self):
        print "method : ",self.test

test_o = Exam("Fine")

print dir(test_o)

print Exam.test
print test_o.test
test_o.test()
Run Code Online (Sandbox Code Playgroud)

输出:

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',    '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'test']
<unbound method load.test>
Fine
Traceback (most recent call last):
  File "example.py", line 32, in <module>
    test_o.test()
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)

怎么打电话

  1. class属性, Exam.test- > <unbound method load.test>output显示方法 …

python methods instance-variables class-attributes

25
推荐指数
2
解决办法
1万
查看次数

python-pyramid app内存根本没有发布

如何解决这个内存泄漏?

我应该采取什么措施来清理旧的会话对象?是不是session.close()足够?

要么

这与金字塔有关吗?

Sqlalchmey setup:
----------------------------------------------------------------------------------
def get_db(request):
    maker = request.registry.dbmaker
    session = maker()

    @profile
    def cleanup(request):
        _session = request.db
        if request.exception is not None:
            _session.rollback()
        else:
            _session.commit()
        _session.close()
        # del _session     # No memory released

    request.add_finished_callback(cleanup)
    return session

def main(global_config, **settings):
    :
    :
    config.registry.dbmaker = sessionmaker(bind=engine)
    config.add_request_method(get_db, name='db', reify=True)
    :
    :
Run Code Online (Sandbox Code Playgroud)

金字塔app请求处理程序就像

@view_config(route_name='list_employees', renderer='json')
def employees(request):
   session = request.db
   office = session.query(Office).get(1)
   employees = [x.name for x in office.employees]
   return employees
Run Code Online (Sandbox Code Playgroud)

现在的问题是,在list_employees的每个请求中,内存都在增长.内存增加的大小几乎等于大小office.employees.

调试:

request 1 …
Run Code Online (Sandbox Code Playgroud)

python memory-leaks sqlalchemy pyramid

7
推荐指数
1
解决办法
917
查看次数

如何在Django和Celery中配置多个代理?

要求:Django使用RabbitMQ(Internal)和SQS / Kafka这两个任务共享通用的DB / Django模型。

截至2016年10月,Django设置仅支持一种代理配置

如何使用不同的队列配置和代理设置共享任务?

django amazon-sqs celery django-settings django-celery

5
推荐指数
1
解决办法
551
查看次数

python全局变量,这里发生了什么?需要说明

有人可以解释这里发生了什么

x = 10
def foo():
    print "x in foo = ",x
    if x: x = 8    -------------> mysterious line

foo()
print "x in main = ",x
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,如果我注释掉神秘的线(如果x:x = 8)

我得到了输出

x in foo =  10
x in main =  10
Run Code Online (Sandbox Code Playgroud)

否则我最终会出错

"UnboundLocalError:赋值前引用的局部变量'x'"

为什么这样?

我知道global x只有在需要在本地修改全局变量时才有用.

python

1
推荐指数
1
解决办法
98
查看次数