如果变量不为空,则解压缩函数的参数

时间:2016-01-11 12:09:21

标签: python conditional-statements argument-unpacking

如果变量不为空,我试图传入一个参数列表。

这是变量:

shape_properties: {
                    'header_shape': {
                                    'width': 8582400,
                                    'height': 468000
                                    },
                    'chart_shape': {
                                    'width': 8582400,
                                    'height': 3585600
                                    }}

但有时这个变量可能是无:

shape_properties=None

如果它不是空的话,我想从shape_properties中引入争论。这是我试过的:

sub_title_shp = add_textbox(slide,
                            text=question_label,
                            **shape_properties['header_shape'] if shape_properties else '') 

我收到此错误:

TypeError: add_textbox() argument after ** must be a mapping, not unicode

错误代码告诉我,我不能在这样的条件下使用** args?

这就是add_textbox()的样子:

def add_textbox(
          sld, text, 
          left=Emu(0), top=Emu(0), width=Emu(300), height=Emu(100),
          font_name="Calibri",
          font_size=12, 
          font_bold=True,
          font_italic=False,
          font_color=(89,89,89),
          font_color_brightness=0,
          font_color_theme=None,
          word_wrap=True, 
          auto_size=None, 
          fit_text=True,
          font_file=None,
          margin_left=0.25,
          margin_right=0.25,
          margin_top=0.13,
          margin_bottom=0.13, 
          vertical_alignment='top',
          horizontal_alignment='left',
          textbox_fill_solid=False,
          textbox_color=(100,0,0),
          textbox_color_brightness=0,
          ):

1 个答案:

答案 0 :(得分:2)

试试这个;)

sub_title_shp = add_textbox(slide,
                        text=question_label,
                        **(shape_properties['header_shape'] if shape_properties else {}))