Python 3:如何从另一个文件调用函数并将参数传递给该函数?

时间:2016-09-17 07:14:10

标签: python function python-3.x

我想从另一个文件中调用一个函数,并将当前文件中的参数传递给该文件。以下示例,在文件 bye.py 中,我想从“ hi.py ”文件中调用“ me ”函数并传递“再见“字符串功能”“。怎么做 ?谢谢:))

我有文件hi.py

def me(string):
    print(string)
me('hello')

bye.py

from hi import me
me('goodbye')

我得到了什么:

hello
goodbye

我想要的是什么:

goodbye

1 个答案:

答案 0 :(得分:1)

通常,在创建要导入的文件时,必须使用def me(string): print(string) if __name__ == '__main__': # Do some local work which should not be reflected while importing this file to another module. me('hello') ,如果从其他文件导入文件,则cart.attributes的结果为false。所以你的hi.py可能看起来像:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rootview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"

                app:popupTheme="@style/MyMaterialTheme.PopupOverlay">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:paddingRight="16dp">

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="16dp"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:text=""
                        android:textColor="#fff"
                        android:textSize="20.4sp"
                        android:transitionName="profile" />

                    <ImageButton
                        android:id="@+id/options"

                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@null"


                        />
                </LinearLayout>

            </android.support.v7.widget.Toolbar>


            <android.support.v7.widget.RecyclerView
                android:id="@+id/chat_list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:background="#efefef"
                android:scrollbars="none"
                app:reverseLayout="true"

                app:stackFromEnd="true" />

    <RelativeLayout
            android:id="@+id/design_bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"

            app:behavior_hideable="true"
            app:behavior_peekHeight="50dp"

            app:layout_behavior="@string/bottom_sheet_behavior">

            <include layout="@layout/recommendation_keyboard_paginated"></include>

        </RelativeLayout>
        </LinearLayout>



    </android.support.design.widget.CoordinatorLayout>
相关问题