在构建器调用时将值附加到环境变量 [英] append value to environment variable on builder call

查看:41
本文介绍了在构建器调用时将值附加到环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题如下:我有一个环境,其中定义了一些变量,如下所示:

The problem is as follows: I have an environment with some variables defined like this:

env = Environment(CPPPATH=['#/include'])

在某些情况下,我需要调用带有一些额外值的构建器,这些值不应永久添加到环境中,以免造成不必要的污染.

In some cases I need to invoke a builder with some extra values which should not be added permanently to the environment to not unnecessarily pollute it.

一种方法是通过将额外的值与环境的值合并来将额外的值附加到构建器调用中.

One way is to append the extra value to the builder call by merging it with the environment's value.

env.Object('test.c', CPPPATH=['#/some_other_include_path']+env['CPPPATH'])

有没有更优雅的方法来做到这一点?

Is there a more elegant way to do it?

推荐答案

我通过克隆 env 并附加到它来做到这一点,如下所示:

I do this by cloning the env and appending on to it, like this:

clonedEnv = env.Clone()
clonedEnv.Append(CPPPATH=['#anotherPath'])
clonedEnv.Object('test.c')

一种更pythonic(和有效)的方式来做你正在做的事情是使用python list.extend() 函数:

A more pythonic (and efficient) way to do what you are doing would be to use the python list.extend() function:

cpppath = ['path1', 'path2']
cpppath.extend(env['CPPPATH'])
env.Object('test.c', CPPPATH = cpppath)

这篇关于在构建器调用时将值附加到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆