a = [1, 2, 3, 4]
b = a.index(6)
del a[b]
print a
Run Code Online (Sandbox Code Playgroud)
以上显示以下错误:
Traceback (most recent call last):
File "D:\zjm_code\a.py", line 6, in <module>
b = a.index(6)
ValueError: list.index(x): x not in list
Run Code Online (Sandbox Code Playgroud)
所以我必须这样做:
a = [1, 2, 3, 4]
try:
b = a.index(6)
del a[b]
except:
pass
print a
Run Code Online (Sandbox Code Playgroud)
但有没有更简单的方法来做到这一点?
什么是related_name
对on ManyToManyField
和ForeignKey
字段有用的参数?例如,给出以下代码,有什么影响related_name='maps'
?
class Map(db.Model):
members = models.ManyToManyField(User, related_name='maps',
verbose_name=_('members'))
Run Code Online (Sandbox Code Playgroud) 我在某人的代码中看到了这个.这是什么意思?
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.stream.close()
Run Code Online (Sandbox Code Playgroud)
from __future__ import with_statement#for python2.5
class a(object):
def __enter__(self):
print 'sss'
return 'sss111'
def __exit__(self ,type, value, traceback):
print 'ok'
return False
with a() as s:
print s
print s
Run Code Online (Sandbox Code Playgroud) a='aaaa'
print isinstance(a, basestring)#true
print isinstance(a, str)#true
Run Code Online (Sandbox Code Playgroud) var a=$('#start > div:last-child');
var b=$('#start > div.live')[0];
alert(a==b)
alert(a==$(b))
Run Code Online (Sandbox Code Playgroud)
这总是错误的.你如何比较jQuery中的两个元素?
谢谢
def __repr__(self):
return '<%s %s (%s:%s) %s>' % (
self.__class__.__name__, self.urlconf_name, self.app_name,
self.namespace, self.regex.pattern)
Run Code Online (Sandbox Code Playgroud)
这种方法的意义/目的是什么?
这段代码是在b.py中获取templates/blog1/page.html:
path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'blog1/page.html'))
Run Code Online (Sandbox Code Playgroud)
但我想获得父目录位置:
aParent
|--a
| |---b.py
| |---templates
| |--------blog1
| |-------page.html
|--templates
|--------blog1
|-------page.html
Run Code Online (Sandbox Code Playgroud)
以及如何获得aParent位置
谢谢
更新:
这是正确的:
dirname=os.path.dirname
path = os.path.join(dirname(dirname(__file__)), os.path.join('templates', 'blog1/page.html'))
Run Code Online (Sandbox Code Playgroud)
要么
path = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
Run Code Online (Sandbox Code Playgroud) 在django.utils.functional.py
:
for t in type(res).mro(): # <----- this
if t in self.__dispatch:
return self.__dispatch[t][funcname](res, *args, **kw)
Run Code Online (Sandbox Code Playgroud)
我不明白mro()
.它是做什么的,"mro"是什么意思?
python ×9
django ×1
dom ×1
file ×1
file-rename ×1
javascript ×1
jquery ×1
list ×1
many-to-many ×1
oop ×1
path ×1
syntax ×1