Skip to main content
 首页 » 编程设计

python之如果变量不为空,则将参数解包到函数

2024年10月24日10bjzhanghao

如果变量不为空,我会尝试将参数列表传递给函数。

这是变量:

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

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

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, 
          ): 

请您参考如下方法:

试试这个 ;)

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