Why can't I use

I've seen examples online of people using __getattr__ with Django models, but whenever I try I get errors. (Django 1.2.3) I don't have any problems when I am using __getattr__ on normal objects. For example: class Post(object): def __getattr__(self, name): return 42 Works just fine... >>> from blog.models import Post >>> p = Post() >>> p.

为什么我不能使用

我在网上看到使用__getattr__和Django模型的人的例子,但是每当我尝试我会得到错误。 (Django 1.2.3) 当我在普通对象上使用__getattr__时,我没有任何问题。 例如: class Post(object): def __getattr__(self, name): return 42 工作得很好... >>> from blog.models import Post >>> p = Post() >>> p.random 42 现在,当我尝试使用Django模型时: from django.db impo

Where should I put helper functions for scripts that utilize a common module?

I've been developing a set of Python scripts that, to keep things simple, manipulate a set of input files using a parser that I wrote specifically for those scripts. The project's design generally looked like this: /project/project/__init__.py /project/project/parser.py /project/project/helperfunctions.py /project/preferences.ini /project/script1.py /project/script2.py Each script call

我应该在哪里为使用通用模块的脚本添加辅助函数?

我一直在开发一组Python脚本,为了简单起见,我使用专门为这些脚本编写的解析器来处理一组输入文件。 该项目的设计通常如下所示: /project/project/__init__.py /project/project/parser.py /project/project/helperfunctions.py /project/preferences.ini /project/script1.py /project/script2.py 每个脚本都会调用helperfunctions.py某些函数来执行诸如读取首选项文件之类的任务 - 对于每个脚本都必须重复的代码。 根本

Should I put #! (shebang) in Python scripts, and what form should it take?

Should I put the shebang in my Python scripts? In what form? #!/usr/bin/env python or #!/usr/local/bin/python Are these equally portable? Which form is used most? Note: the tornado project uses the shebang. On the other hand the Django project doesn't. The shebang line in any script determines the script's ability to be executed like a standalone executable without typing pyth

我应该放#! (shebang)在Python脚本中,它应该采用什么形式?

我应该把shebang放到我的Python脚本中吗? 以什么形式? #!/usr/bin/env python 要么 #!/usr/local/bin/python 这些是否同样便携? 哪种形式最常用? 注意:龙卷风项目使用shebang。 另一方面,Django项目没有。 任何脚本中的shebang行决定了脚本能够像独立可执行文件一样执行,而无需事先在终端中键入python ,或者在文件管理器中双击它(如果配置正确)。 这是没有必要的,但通常会放在那里,所以当有人看到在编

What is the Python equivalent of static variables inside a function?

What is the idiomatic Python equivalent of this C/C++ code? void foo() { static int counter = 0; counter++; printf("counter is %dn", counter); } specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything? A bit reversed, but this should work: def foo(): foo.counter +=

什么是函数内部的静态变量的Python等价物?

这个C / C ++代码的惯用Python等价物是什么? void foo() { static int counter = 0; counter++; printf("counter is %dn", counter); } 具体而言,如何在功能级别实现静态成员,而不是类级别? 并且将函数放入类中是否会改变任何内容? 有点逆转,但这应该工作: def foo(): foo.counter += 1 print "Counter is %d" % foo.counter foo.counter = 0 如果你想在顶部而不是底部计数器初始化代码,你可

Using Pylint with Django

I would very much like to integrate pylint into the build process for my python projects, but I have run into one show-stopper: One of the error types that I find extremely useful--: E1101: *%s %r has no %r member* --constantly reports errors when using common django fields, for example: E1101:125:get_user_tags: Class 'Tag' has no 'objects' member which is caused by this code: def get_user_tag

在Django中使用Pylint

我非常想将pylint整合到我的python项目的构建过程中,但是我遇到了一个显示限制器:我发现其中一种非常有用的错误类型 - : E1101: *%s %r has no %r member*在使用常用django字段时不时报告错误,例如: E1101:125:get_user_tags: Class 'Tag' has no 'objects' member 这是由以下代码引起的: def get_user_tags(username): """ Gets all the tags that username has used. Returns a query set. """ return

What's the point of @staticmethod in Python?

I've developed this short test/example code, in order to understand better how static methods work in Python. class TestClass: def __init__(self, size): self.size = size def instance(self): print("regular instance method - with 'self'") @staticmethod def static(): print("static instance method - with @staticmethod") def static_class(): p

Python中的@staticmethod有什么意义?

我已经开发了这个简短的测试/示例代码,以更好地理解静态方法在Python中的工作方式。 class TestClass: def __init__(self, size): self.size = size def instance(self): print("regular instance method - with 'self'") @staticmethod def static(): print("static instance method - with @staticmethod") def static_class(): print("static class method") a =

Why use staticmethod instead of no decorator at all

There are several good explanations on SO about why/when you should use a class method vs a static method, but I've not been able to find an answer for when you would use a static method over no decoration at all. Consider this class Foo(object): @staticmethod def f_static(x): print("static version of f, x={0}".format(x)) def f_standalone(x): print("stan

为什么使用staticmethod而不是装饰器?

关于为什么/何时应该使用类方法vs静态方法,有几个很好的解释,但是我一直无法找到什么时候使用静态方法而没有装饰的答案。 考虑这一点 class Foo(object): @staticmethod def f_static(x): print("static version of f, x={0}".format(x)) def f_standalone(x): print("standalone verion of f, x={0}".format(x)) 还有一些输出: >>> F = Foo >>> f = F() >>

How to iterate over Unicode characters in Python 3?

I need to step through a Python string one character at a time, but a simple "for" loop gives me UTF-16 code units instead: str = "abcu20acU00010302U0010fffd" for ch in str: code = ord(ch) print("U+{:04X}".format(code)) That prints: U+0061 U+0062 U+0063 U+20AC U+D800 U+DF02 U+DBFF U+DFFD when what I wanted was: U+0061 U+0062 U+0063 U+20AC U+10302 U+10FFFD Is there any way

如何迭代Python 3中的Unicode字符?

我需要一次一个字符地浏览一个Python字符串,但是一个简单的“for”循环会给我一个UTF-16代码单元: str = "abcu20acU00010302U0010fffd" for ch in str: code = ord(ch) print("U+{:04X}".format(code)) 打印: U+0061 U+0062 U+0063 U+20AC U+D800 U+DF02 U+DBFF U+DFFD 当我想要的是: U+0061 U+0062 U+0063 U+20AC U+10302 U+10FFFD 有没有什么办法可以让Python给我一系列的Unicode代码点,而不管字符串是如何在

Can't add field to ModelForm at

I have a problem with ModelForm. Field "test1" is displayed, but "test2" - is not. Playing with base_fields didn't help. # models.py class Country(models.Model): name = CharField(max_length=100) # admin.py class CountryAdminForm(ModelForm): test1 = forms.CharField('test1') def __init__(self, *args, **kwargs): super(CountryAdminForm, self).__init__

无法将字段添加到ModelForm处

我有一个ModelForm的问题。 显示字段“test1”,但“test2” - 不是。 玩base_fields没有帮助。 # models.py class Country(models.Model): name = CharField(max_length=100) # admin.py class CountryAdminForm(ModelForm): test1 = forms.CharField('test1') def __init__(self, *args, **kwargs): super(CountryAdminForm, self).__init__(*args, **kwargs) self.fields['test2'] = forms.Char

Django foreign key access in save() function

Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True, ) url = models.URLField() def __unicode__(self): return self.name def save(self): pass class Item(models.Model): publisher = models.ForeignKey(Publisher) name = models.CharField( max_length = 200, )

Django在save()函数中的外键访问

这是我的代码: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True, ) url = models.URLField() def __unicode__(self): return self.name def save(self): pass class Item(models.Model): publisher = models.ForeignKey(Publisher) name = models.CharField( max_length = 200, ) c