V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  wisefree  ›  全部回复第 15 页 / 共 16 页
回复总数  304
1 ... 7  8  9  10  11  12  13  14  15  16  
2016-12-20 18:10:21 +08:00
回复了 wisefree 创建的主题 Python 请教 python 中抽象类和抽象方法相关问题
@practicer 嗯,我也看了这本书,文档中有这样一段话:
@abc.abstractmethod
A decorator indicating abstract methods.
Using this decorator requires that the class ’ s metaclass is ABCMeta or is derived from it.


但是如下代码运行没有问题:

```python

from abc import ABCMeta, abstractmethod

class Shape(object):

@abstractmethod
def area(self):
pass

def __lt__(self, obj):
return self.area() < obj.area()

def __eq__(self, obj):
return self.area() == obj.area()


class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h

def area(self):
return self.w * self.h


if __name__ == "__main__":
r1 = Rectangle(2, 3)
r2 = Rectangle(2, 4)
print(r1 < r2)

```


如下代码也没有问题

``` python
from abc import ABCMeta, abstractmethod

class Shape(metaclass=ABCMeta):

@abstractmethod
def area(self):
pass

def __lt__(self, obj):
return self.area() < obj.area()

def __eq__(self, obj):
return self.area() == obj.area()


class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h

def area(self):
return self.w * self.h


if __name__ == "__main__":
r1 = Rectangle(2, 3)
r2 = Rectangle(2, 4)
print(r1 < r2)
```
2016-12-19 10:05:53 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@wwqgtxx 谢谢!答案太精彩了!越往下学,好像内容越来越多了,~_~
2016-12-19 08:26:46 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@wwqgtxx 谢谢!现在我在终端运行代码,终端退出和未退出的情形让我有点困惑。

运行环境: virtualbox ubuntu ipython3.5.1
我在 ipython 控制台中,打开了两个终端,我尝试了一下如下代码

终端 1 : ipython (还未退出)
``` python
f = open('test.txt', 'w', buffering=4)

f.write('+'*4)

f.write('+')
```
终端 2
tail -f test.txt

但是 test.txt 没有出现+


在终端 1 中输入 exit ,退出 ipython
终端 2 出现了 5 个+


通过你们的解释,这个我能理解了,程序退出强制 flush

但是程序没有退出,为什么没有进行 flush 呢?
2016-12-19 08:25:26 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@mringg 确实如此,谢啦!

运行环境: virtualbox ubuntu ipython3.5.1
我在 ipython 控制台中,打开了两个终端,我尝试了一下如下代码

终端 1 : ipython (还未退出)
``` python
f = open('test.txt', 'w', buffering=4)

f.write('+'*4)

f.write('+')
```
终端 2
tail -f test.txt

但是 test.txt 没有出现+


在终端 1 中输入 exit ,退出 ipython
终端 2 出现了 5 个+


通过你们的解释,这个我能理解了,程序中强制 flush

但是程序没有退出,为什么没有进行 flush 呢?
2016-12-19 08:18:52 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@SlipStupig 谢啦,但是帮助文档的解释不能回答我的问题
2016-12-18 18:55:55 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@wwqgtxx 嗯嗯,确实如此,谢谢啦。 sleep(10), 10 秒内里面没有内容, 10s 后里面也没有, f.flush()才有++++
为什么要自动 flush 呢?这个真心让人困惑
2016-12-18 18:53:53 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@Kisesy 10 秒内看看里面没有,就算 10s 后里面也没有, f.flush()后才有,:),谢谢!
2016-12-18 14:58:53 +08:00
回复了 wisefree 创建的主题 Python 怎么设置 python 中 open 的 buffering 参数
@a87150 不是这样的。我按照 python 帮助文档定义进行操作,没有得到预期的结果
2016-12-16 20:39:33 +08:00
回复了 wisefree 创建的主题 Python 迭代器和可迭代对象的疑问
@introom 写着写着,人懵圈了。。。
```python
def __iter__(self):
return self
```
iter(myRange)返回的是自己 myRange 。。。
2016-12-16 20:38:13 +08:00
回复了 wisefree 创建的主题 Python 迭代器和可迭代对象的疑问
@introom 谢谢!
2016-11-09 20:45:16 +08:00
回复了 Tianny 创建的主题 Python 请教大家关于 python 类属性和实例属性的问题,谢谢!
继续引发一点思考

```python
#coding:utf-8

class Foo(object):
x = {1:'hello'}


foo = Foo()

foo.x = {1:'world'}

print(foo.x)
print(Foo.x)


```

输出:
{1: 'world'}
{1: 'hello'}
2016-11-09 20:43:49 +08:00
回复了 Tianny 创建的主题 Python 请教大家关于 python 类属性和实例属性的问题,谢谢!
'''python
#coding:utf-8

class Foo(object):
x = {1:'hello'}


foo = Foo()

foo.x[1] = 'world'
print(foo.__dict__)
'''
输出:
{}
说明, foo.x[1] = 'world',根本没有创建一个实例属性
2016-11-02 18:12:31 +08:00
回复了 wisefree 创建的主题 Python matplotlib 如何解决中英混排
@justou 网上搜的结果,全部没有这个方法方便,谢谢啦:)
2016-11-02 16:39:52 +08:00
回复了 wisefree 创建的主题 Python matplotlib 如何解决中英混排
@justou 如何得到 fonts 对应的文件名呢? simusun.ttc 在 fonts 文件夹下看不到
网上搜索了一下,请问用 python 能实现么?
2016-10-29 19:20:18 +08:00
回复了 wisefree 创建的主题 Python python os.fork(),有关子进程入口的疑问
@firstway
:),感谢!
2016-10-29 19:20:03 +08:00
回复了 wisefree 创建的主题 Python python os.fork(),有关子进程入口的疑问
@msg7086
受教了,谢谢!
2016-10-28 13:31:53 +08:00
回复了 wisefree 创建的主题 Python python os.fork(),有关子进程入口的疑问
@BOYPT 这个是啥,没见到过,粘贴到代码里运行不了。。。
> NameError: name '__FILE__' is not defined
2016-10-28 13:28:56 +08:00
回复了 wisefree 创建的主题 Python python os.fork(),有关子进程入口的疑问
@sujin190
@enenaaa
确实如此,针对 linux fork , google 了一下,
>fork 英文意思:分支, fork 系统调用复制产生的子进程与父进程(调用进程)基本一样:代码段+数据段+堆栈段+PCB ,当前的运行环境基本一样,所以子进程在 fork 之后开始向下执行,而不会从头开始执行。
>https://www.cnblogs.com/mickole/p/3186441.html

以后还是用 multiprocessing>_<
2016-10-28 13:23:47 +08:00
回复了 wisefree 创建的主题 Python python os.fork(),有关子进程入口的疑问
@EchoUtopia 额,其实我也是这样困惑的。。
2016-10-28 13:23:30 +08:00
回复了 wisefree 创建的主题 Python python os.fork(),有关子进程入口的疑问
@raysonx 谢谢提醒!我会去看看的
1 ... 7  8  9  10  11  12  13  14  15  16  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2265 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 20ms · UTC 10:16 · PVG 18:16 · LAX 03:16 · JFK 06:16
Developed with CodeLauncher
♥ Do have faith in what you're doing.