开发者

Python: define multiple variables of same type?

开发者 https://www.devze.com 2023-01-29 18:22 出处:网络
Probably a duplicate,开发者_StackOverflow社区 but I can\'t find the answer by searching with these terms, at least.

Probably a duplicate,开发者_StackOverflow社区 but I can't find the answer by searching with these terms, at least.

Is there a quicker way to do this in Python?

level1 = {}
level2 = {}
level3 = {}

I've tried

level1 = level2 = level3 = {}

But that seems to create copies of the object, which isn't what I want. And

level1, level2, level3 = {}

throws an error.


level1 = level2 = level3 = {}

Doesn’t create copies. It lets reference level{1-3} to the same object. You can use a list comprehension instead:

level1, level2, level3 = [{} for dummy in range(3)]

or more readable:

level1, level2, level3 = {}, {}, {}


Your variable naming is a possible sign that your design could be improved. It might be better to use a list instead of three separate variables:

levels = [{}, {}, {}]


You could do

level1, level2, level3 = {}, {}, {}


level1, level2, level3, = {}, {}, {}


I personally never change code to squeeze more stuff on one line. Have three different assignments is the way I would do it, one per line.

However, if there are documented performance improvements by making such a change, that's another thing. I just don't know of any performance improvements with these changes. Only obfuscation.


People have already answered the intent of your question, but...

Is there a quicker way to do this in Python?

What do you mean by quicker?

I think your real question should be "what is the pythonic way to do this?"

level1, level2, level3 = {}

From the Python documentation:

Tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as a, b, c or (). A single item tuple must have a trailing comma, such as (d,).

a, b, c is implicitly (a, b, c). That's why you need to have a 3-tuple on the right side as suggested by other posters (nils, sdolan, gunner).

Shown explicitly they are suggesting this:

(level1, level2, level3) = ({}, {}, {},)


Python is interpreted which allows the definitions of things to be made at runtime.

def prefixed_range(prefix, start, stop=None, step=1):
    " Like xrange() but results are prefixed numeric strings. """
    prefix = str(prefix)
    if stop is None:
        start, stop = 0, start
    for i in xrange(start, stop, step):
        yield prefix + str(i)

def define_vars(names, value=None, namespace=None):
    namespace = globals() if not namespace else namespace
    factory = (lambda: value) if not callable(value) else value
    for name in names:
        namespace[name] = factory()

define_vars(prefixed_range('level', 1, 4), dict)

print 'level1: {}, level2: {}, level3: {}'.format(level1, level2, level3)
# level1: {}, level2: {}, level3: {}


Try this: level1,level2,level3=[{}]*3

0

精彩评论

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