Geo
Geo

Reputation: 96837

Can I import as only for a function, and have the rest imported as they are?

Let's say I have a function named "hello" in a module named a, and various other functions. Is it possible to import hello as goodbye, along with the other symbols? I am thinking of something like this, however it's not valid:

from a import hello as goodbye,*

Upvotes: 2

Views: 618

Answers (4)

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29111

Next two lines work fine for me:

from core.commonActions import click_on_toolbar_tool, wait_toolbar_tool_enabled as x
from core.commonActions import wait_toolbar_tool_enabled as x, click_on_toolbar_tool

OR if you need to import all functions you can use:

from core.commonActions import *
from core.commonActions import wait_toolbar_tool_enabled as x

OR:

from core.commonActions import *
x = wait_toolbar_tool_enabled

AND if you want hello not to be more available then simply:

from core.commonActions import *
x = wait_toolbar_tool_enabled
wait_toolbar_tool_enabled = None # or del wait_toolbar_tool_enabled

Upvotes: 1

X-Istence
X-Istence

Reputation: 16667

from a import *
goodbye = hello
del hello

Would be another way to do it.

Upvotes: 1

Francesco
Francesco

Reputation: 3250

You can import from a, then bind the new name that you want and delete the previous. Something like

from a import *
goodbye = hello
del hello

Star imports are usually not so good exactly because of namespace pollution.

Upvotes: 7

user2665694
user2665694

Reputation:

from a import *
from a import hello as goodbye

Upvotes: 2

Related Questions