是否可以将函数放入一个新模块中,该模块从主模块调用函数/对象?
from xxx import some
from yyy import hello
from funtions import function_y
object = some.thing()
def function_x():
dostuff...
hello(x, y)
function_y()
def function_y():
dostuff...
object.bye() #object from Main.py
dostuff...
function_x() #function from Main.py
如果我这样做的话。我明白了:
NameError: name 'object' is not defined
但是,如果我尝试将Main.py导入到functions.py中,那就更糟了.. 是..你可以看到..我是一个新手...... :-)对不起。我只想将我的代码组织成碎片。为每一个 话题另一个* .py ......但也许我错了?并且必须将从主命名空间调用某些内容的每个代码放入Main.py中?除非我可以将对象作为参数传递?
答案 0 :(得分:0)
我建议遵循以下结构。
<强> functions.py 强>
from yyy import hello
def function_x():
dostuff...
hello(x, y)
def function_y(object):
dostuff...
object.bye()
dostuff...
function_x()
<强> main.py 强>
from xxx import some
from funtions import function_x, function_y
object = some.thing()
function_y(object)
甚至在分离的包中包装function_x和function_y。如果不了解目的,就很难做出决定。
答案 1 :(得分:0)
看起来您应该能够像这样构建代码:
from xxx import some
import functions
object = some.thing()
functions.function_y(object)
from yyy import hello
def function_x():
dostuff...
hello(x, y)
def function_y(obj):
dostuff...
obj.bye() #object passed as parameter
dostuff...
function_x() #function now local in function.py
答案 2 :(得分:0)
如果您从public class ContactsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init the views and setup the searchbar
setupMultiContactsPicker();
}
//... Some other stuff like an OnClick implementation and
//other utility methods that only manage visibility of some
//components of the UI like 2 TextViews and a ProgressBar
private void setupMultiContactsPicker() {
final Filter.FilterListener filterListener = new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
progressBar.setVisibility(View.GONE);
updateLayout();
}
};
unselectedContactsAdapter = new UnselectedContactsAdapter();
unselectedContactsAdapter.setContactCallback(new ContactCallback() {
@Override
public void onContactInserted(Contact contact, int adapterPosition) {
selectedContactsAdapter.setPropagate(false);
if(selectedContactsAdapter.getItemCount() > 0) selectedContactsAdapter.remove(contact);
selectedContactsAdapter.setPropagate(true);
updateLayout();
}
@Override
public void onContactRemoved(Contact contact, int adapterPosition) {
selectedContactsAdapter.setPropagate(false);
selectedContactsAdapter.add(contact);
selectedContactsAdapter.setPropagate(true);
updateLayout();
}
@Override
public void onContactMoved(Contact contact, int from, int to) {
}
@Override
public void onContactChanged(Contact contact, int adapterPosition) {
}
});
unselectedContacsRV.setVisibility(View.VISIBLE);
LinearLayoutManager uLayoutManager = new LinearLayoutManager(ContactsActivity.this, LinearLayoutManager.VERTICAL, false);
unselectedContacsRV.setLayoutManager(uLayoutManager);
unselectedContacsRV.setAdapter(unselectedContactsAdapter);
unselectedContacsRV.setHasFixedSize(true);
// Initisialize the adapter with the correct contacts (ContactsFilter.UNSELECTED)
unselectedContactsAdapter.getFilter().filter(FilterableContactsAdapter.ContactsFilter.UNSELECTED, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
onLoadFinish(); //This method call updateLayout(); when the 2 filters finish to load data into the adapters.
}
});
selectedContactsAdapter = new SelectedContactsAdapter();
selectedContactsAdapter.setContactCallback(new ContactCallback() {
@Override
public void onContactInserted(Contact contact, int adapterPosition) {
unselectedContactsAdapter.setPropagate(false);
if(unselectedContactsAdapter.getItemCount() > 0) unselectedContactsAdapter.remove(contact);
unselectedContactsAdapter.setPropagate(true);
updateLayout(); // This method show or hide some TextViews that I use as section titles, nothing more
}
@Override
public void onContactRemoved(Contact contact, int adapterPosition) {
unselectedContactsAdapter.setPropagate(false);
unselectedContactsAdapter.add(contact);
unselectedContactsAdapter.setPropagate(true);
updateLayout(); // This method show or hide some TextViews that I use as section titles, nothing more
}
@Override
public void onContactMoved(Contact contact, int from, int to) {
}
@Override
public void onContactChanged(Contact contact, int adapterPosition) {
}
});
LinearLayoutManager sLayoutManager = new LinearLayoutManager(ContactsActivity.this, LinearLayoutManager.VERTICAL, false);
selectedContacsRV.setLayoutManager(sLayoutManager);
selectedContacsRV.setAdapter(selectedContactsAdapter);
selectedContacsRV.setHasFixedSize(true);
// Initisialize the adapter with the correct contacts (ContactsFilter.SELECTED)
selectedContactsAdapter.getFilter().filter(FilterableContactsAdapter.ContactsFilter.SELECTED, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
onLoadFinish();
}
});
}
}
内导入Main.py
,则可以按照自己的方式进行操作:
function_y()