With的用法?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了With的用法?相关的知识,希望对你有一定的参考价值。

My parents never compare me with other kinds里面的with是做什么语

With的五大用法:

with的用法1:with表示状态时,还可作“跟上…”“听懂…的话”解,一般用于疑问句或否定句中。

with的用法2:with表示关系时还可作“与…合并〔混合,组合〕”解。

with的用法3:with表示伴随状态时,作“以与…同样的方向〔程度,比率〕”解。可接“名词+动词不定式”“名词+现在分词”“名词+过去分词”。

with的用法4:with表示比较时作“同…相比”“与…平行”解。

with的用法5:with可以用来表示虚拟语气,意思是“如果,假如”。用于诗歌或民谣的副歌、叠句中, with常无实际含意。

with的用法例句:

1. Beauty is an attitude. It has nothing to do with age.

美是一种态度,与年龄无关。

2. If you\'re not satisfied with the life you\'re living, don\'t just complain. Do something about it.

对于现况的不满,不能只是抱怨,要有勇气作出改变。

3. He was well acquainted with the literature of France, Germany and Holland.

他对于法国、德国和荷兰的文学了如指掌。

4. I thought I\'d enrol you with an art group at the school.

我想我会吸收你参加学校的一个艺术团。

5. Somehow Karin managed to cope with the demands of her career.

卡琳设法达到了其职业的要求。

6. She ran away with a man called McTavish last year.

去年,她和一个叫麦克塔维什的男人私奔了。

7. I would prefer him to be with us next season.

我更希望他下一个赛季和我们在一起。

8. I recall many discussions with her on these and kindred topics.

我回想起多次同她就这些问题及类似话题进行的讨论。

9. He wiped away the blood with a paper napkin.

他用纸巾将血迹擦去。

10. 30 percent of reptiles, birds, and fish are currently threatened with extinction.

目前,30%的爬行动物、鸟类和鱼类面临灭绝的危险。

11. I found myself behind a curtain, necking with my best friend\'swife.

我意识到自己在帘子后面吻着至友的妻子。

12. She spent a period of time working with people dying of cancer.

她有一段时间曾帮助垂危的癌症患者。

13. "I think I\'ll try a hypnotist," I said with sudden resolution.

“我会找催眠师试试,”我突然坚决地说。

14. Season with salt, pepper and a pinch of cayenne.

调以盐、胡椒和少许辣椒粉。

15. The economy remains deep in recession with few signs of a pick-up.

经济仍深陷衰退之中,几乎没有好转的迹象。

参考技术A 句子"My parents never compare me with other kinds"中,with是一个介词,用来表示比较的对象。在这个句子中,with后面的"other kinds"指代其他孩子,表示父母从不把你和其他孩子做比较。

如何用python 中with 用法

要使用 with 语句,首先要明白上下文管理器这一概念。有了上下文管理器,with 语句才能工作。
下面是一组与上下文管理器和with 语句有关的概念。
上下文管理协议(Context Management Protocol):包含方法 __enter__() 和 __exit__(),支持
该协议的对象要实现这两个方法。
上下文管理器(Context Manager):支持上下文管理协议的对象,这种对象实现了
__enter__() 和 __exit__() 方法。上下文管理器定义执行 with 语句时要建立的运行时上下文,
负责执行 with 语句块上下文中的进入与退出操作。通常使用 with 语句调用上下文管理器,
也可以通过直接调用其方法来使用。
运行时上下文(runtime context):由上下文管理器创建,通过上下文管理器的 __enter__() 和
__exit__() 方法实现,__enter__() 方法在语句体执行之前进入运行时上下文,__exit__() 在
语句体执行完后从运行时上下文退出。with 语句支持运行时上下文这一概念。
上下文表达式(Context Expression):with 语句中跟在关键字 with 之后的表达式,该表达式
要返回一个上下文管理器对象。
参考技术A

在Python中,如果一个对象有__enter__和__exit__方法,就可以在with语句中使用它。

with块结束时会调用相应的__exit__中的代码.因此,我们不需要再写相应的代码去close,无论是因为什么原因结束with.

with open(...) as f:
    print(f.readall())

等价于:

f = open(...)
print(f.readal())
f.close()

同时,我们还可以在一个with 语句中包括多个对象:

with open(...) as f1, open(...) as f2:
    ...

如果不使用with,考虑到f2可能会打开失败或者后续的操作会出错,我们可以需要这样去写:

f1 = open(...)
try:
    f2 = open(...)
    ...
catch:
    pass
else:
    f2.close()
f1.close()

以上是关于With的用法?的主要内容,如果未能解决你的问题,请参考以下文章

SQL 中with的用法

Oracle中with as的用法

PL/SQL with as 用法。

with as 用法

如何用python 中with 用法

DB2 with的定义与用法