可以访问Class_object的名称.__name__,参见代码:
>>> object
<class 'object'>
>>> object.__name__
'object'
Run Code Online (Sandbox Code Playgroud)
不过,__name__方法 is not inclass_object的默认设置.
代码:
>>> foo = dir(object)
>>> foo
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> foo.count('__name__')
0 # '__name__' is not in list
Run Code Online (Sandbox Code Playgroud)
object是所有类的基础.它具有所有Python类实例共有的方法.
哪里__name__的设置位于?
我collectstatic用命令禁用
heroku config:set DISABLE_COLLECTSTATIC=1\nRun Code Online (Sandbox Code Playgroud)\n\n成功将我的项目推送到 Heroku 后,手动collectstatic如下:
$ heroku run python manage.py collectstatic\nRun Code Online (Sandbox Code Playgroud)\n\n不幸的是,Heroku 报告错误涉及manage.py
Running python manage.py collectstatic on \xe2\xac\xa2 fierce-cove-94300... up, run.6296 (Free)\n/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.\n """)\nTraceback (most recent call last):\n File "manage.py", line 22, in <module>\n execute_from_command_line(sys.argv)\n File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in …Run Code Online (Sandbox Code Playgroud) 在emacs中检查“ most-positive-fixnum”中的变量后,将输出:
most-positive-fixnum is a variable defined in ‘data.c’.
Its value is 2305843009213693951
Documentation:
The largest value that is representable in a Lisp integer.
Run Code Online (Sandbox Code Playgroud)
它是log2值:
In [8]: math.log2(2305843009213693951)
Out[8]: 61.0
Run Code Online (Sandbox Code Playgroud)
为什么设置为as 2 **61而不是2**62or 2**63?
我试图替换列表中的元素
# the raw data
square = ['(', ')', '.', '^', '-']
# the result I want
square = ['[', ']', '.', '^', '-']
Run Code Online (Sandbox Code Playgroud)
使用remove和的方法的多个步骤insert
In [21]: square.remove('(')
In [22]: square.remove(')')
In [23]: square.insert(0, '[')
In [24]: square.insert(1, ']')
In [25]: square
Out[25]: ['[', ']', '.', '^', '-']
Run Code Online (Sandbox Code Playgroud)
如何以一种直截了当的方式解决这样的问题?
在urls.pyDjango
#urls.py
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic')
The second part of the expression, /(?P<topic_id>\d+)/, matches an integer between two forward slashes and stores the integer value in an argument called topic_id.
Run Code Online (Sandbox Code Playgroud)
我尝试用正则表达式来理解它
In [6]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/")
Out[6]: ['1']
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试
In [7]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/").topic_id
AttributeError: 'list' object has no attribute 'topic_id'
Run Code Online (Sandbox Code Playgroud)
似乎整数没有存储topic_id,如何理解呢?
我正在学习SQL"10分钟内的SQL",
引用使用通配符来检索所有记录,它声明:
通常,除非确实需要表中的每一列,否则最好不要使用*通配符.即使使用通配符可以节省显式列出所需列所需的时间和精力,但检索不必要的列通常会降低检索和应用程序的性能.
但是,检索所有记录所花费的时间少于检索多个字段所需的时间:
结果表明,通配符为0.02秒VS 0.1秒
我测试过几次,通配符比多个指定的列不断更快,即使每次消耗的时间都不同.
这可能非常无聊,但是Google搜索确实没有帮助。
在python官方文档中,它不断将文件称为fp:
with open(filename, "w") as fp:
fp.write()
Run Code Online (Sandbox Code Playgroud)
“ p”代表什么?
我正在使用以下命令练习子流程操作:
In [1]: import subprocess
In [3]: cp = subprocess.Popen("cat /etc/group", shell=True, stdout=subprocess.PIPE, stderr=sub
...: process.PIPE).communicate()
In [4]: cp
Out[4]:
(b'root:x:0:\nbin:x:1:\ndaemon:x:2:\nsys:x:3:\nadm:x:4:\ntty:x:5:\ndisk:x:6:\nlp:x:7:\nmem:x:8:\nkmem:x:9:\nwheel:x:10:\ncdrom:x:11:\nmail:x:12:postfix\nman:x:15:\ndialout:x:18:\nfloppy:x:19:\ngames:x:20:\ntape:x:30:\nvideo:x:39:\nftp:x:50:\nlock:x:54:\naudio:x:63:\nnobody:x:99:\nusers:x:100:\nutmp:x:22:\nutempter:x:35:\nssh_keys:x:999:\ninput:x:998:\nsystemd-journal:x:190:\nsystemd-network:x:192:\ndbus:x:81:\npolkitd:x:997:\npostdrop:x:90:\npostfix:x:89:\nchrony:x:996:\nsshd:x:74:\nntp:x:38:\ntcpdump:x:72:\nnscd:x:28:\nnginx:x:995:\nstapusr:x:156:\nstapsys:x:157:\nstapdev:x:158:\ntss:x:59:\nmysql:x:27:\ntest:x:1000:\nscreen:x:84:\n',
b'')
Run Code Online (Sandbox Code Playgroud)
当我尝试解码它时:
In [5]: out = cp.stdout.read().decode("utf-8")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-4f20648dd0bc> in <module>()
----> 1 out = cp.stdout.read().decode("utf-8")
AttributeError: 'tuple' object has no attribute 'stdout'
Run Code Online (Sandbox Code Playgroud)
尝试过替代方案
In [6]: out = cp.decode("utf-8")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-853060f6d163> in <module>()
----> 1 out = cp.decode("utf-8")
AttributeError: 'tuple' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
它如何返回一个元组?我一步步遵循答案python …
我正在学习main并使用代码探索其应用程序:
a = 1
b = 2
def main():
x = add(a, b)
print(x)
if __name__ == "__main__":
main()
def add(a, b):
a = a + 1
return a + b
Run Code Online (Sandbox Code Playgroud)
但是,它报告NameError:
In [87]: run test.py
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
NameError: name 'add' is not defined
Run Code Online (Sandbox Code Playgroud)
重新定位if __name__ == "__main__": 到最终是一个解决方案,
a = 1
b = 2
def main():
x = add(a, b)
print(x)
def add(a, b):
a = a + 1
return a …Run Code Online (Sandbox Code Playgroud) 默认情况下,Typora 将所有图像设置为居中对齐。
我搜索了github.css但没有找到img。
$ grep img "/Users/me/Library/Application Support/abnerworks.Typora/themes/github.css"
#return no matches
Run Code Online (Sandbox Code Playgroud)
如何设置图像左对齐?