开发者

assign multiple instances of a class to variables

开发者 https://www.devze.com 2022-12-20 20:28 出处:网络
Python. I need to assign multiple class instances to number of variables. First i tried this: a = b = c = []

Python. I need to assign multiple class instances to number of variables. First i tried this:

a = b = c = []

but they all refer to the same object, which is not what I need.开发者_如何学运维 This works better:

(a, b, c) = [[] for i in range(3)]

but it seems a bit too verbose. Is there a shorter way to do this?

UPDATE: OK, so this is not really Pythonic to cram everything into one line, question resolved.

But if this worked it'd be really cool:

a,b,c = []*3

And this line creates three references to the same object:

a,b,c = [[]] * 3

Oh well...


a, b, c = [], [], []


simply:

a = []
b = []
c = []

(python is not perl, there is no need for one-liners)


a = []
b = a[:]
import copy
c = copy.copy(b)

You could use these if you want them all initialized to something other than []

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号