Importing a Method From a Specific Class In a Different Module

Sometimes l want to import methods from different modules like in this project(called the Choose Your Own Adventure Wilderness Escape here:

# Importing the Dictwriter() function from the csv module:
from csv import DictWriter

# Importing the datetime class from the datetime module
from datetime import datetime

# Importing the link() function from the os module:
from os import link

# Importing the timezone() function from the pytz module:
from pytz import timezone
...

(630 lines long)

Note these 2 lines:
from datetime import datetime

Another line:
play_again = input(
    f"""
You have finished the game at {datetime.now(timezone('Australia/NSW'))}. Do you want to play again, {name}? Enter yes or no. \n
    """

I wanted to only import the now method from the datetime class. Note: sometimes when I see some code off this platform; I see importing like this:
from module.class import method.
I’m thinking of doing this like this:
from datetime.datetime import now.
Is this recommended?
Edit:
It shows an error:


Now what? :slightly_frowning_face: What syntax should I use?

You need to carefully check what you’re importing. There is a built-in module called datetime, within this module there is class also called datetime which has a method named now-
https://docs.python.org/3/library/datetime.html#datetime-objects

So far as I’m aware you can’t directly import functions bound to a class with just import statements (even when using the from form of import syntax). You can only import module level names/definitions (so module level constants, functions or classes for the most part).

So you’re restricted to importing the module, import datetime and making use of the newly bound name to access its namespace, e.g. datetime.datetime.now(). Or, alternatively, you can import the relevant class definition from this module, from datetime import datetime which imports the whole module but only binds the datetime.datetime class in your current namespace with the name datetime such that you could use datetime.now().

However, there is nothing stopping you from binding a new name to datetime.datetime.now once it has been imported, e.g. newname = datetime.dateimte.now, but be careful about hampering readability if you do this.

If you want more details about import you can see the docs-
https://docs.python.org/3/tutorial/modules.html
Or for a much deeper (but more complex) background see-
https://docs.python.org/3/reference/import.html

1 Like

I this a way to change the name of now?
After that could you call this:
newname(timezone("Australia/NSW"))
as a function instead of of being a method because calling it is decelerating newname to a function object which can be called.
What if you did this:

# Don't worry about this:
from pytz import timezone

import datetime

# Make it so that you can use a different name for the datetime class:
# 1:
times1 = datetime.datetime

# or 2:
from datetime import datetime as times2

# or 3:
now1 = datetime.datetime.now

print(datetime.datetime.now(timezone("Australia/NSW"))

# Do the 3 options have the same effect?
# Is these since I have defined different ways?:
print(times1.now(timezone("Australia/NSW")))
print(times2.now(timezone("Australia/NSW")))
print(now1(timezone("Australia/NSW"))

?
Why did I sometimes see importing like this before:
from something.something import something.
Why is it valid? When I saw the error in the first post I saw it says something about packages. What are those things?

I wouldn’t call it changing a name, just assigning another name to the same object.

Have you tried your examples? You can use it like this because you haven’t changed anything, datetime.datetime.now is still a method (a function belonging to a class) you’ve just assigned another name to the same object.

When nosing around the built-ins (considering just CPython for now) bear in mind that some modules, functions etc. are implemented in C and may not have a direct equivalent in Python; the documentation often tries to provide equivalent pure python code but that is not necessarily what is implemented at run-time (the C version is preferred).

The docs provide a pure Python equivalent for datetime.py (see the first link) utilising @classmethod if you want this behaviour in your own modules (no instances/self but still reliant on the class itself unlike @staticmethod). Hopefully that makes it clear why you can use the datetime.datetime.now method in a slightly different way to what you’re familiar with.

The last example you mention is for packages (a way of organising multiple modules). You can use the dot notation with import for subpackages and submodules (and module levels names of the same with the from syntax). The first link on imports given previously covers packages nicely with examples.

What does this mean:

?

Have you viewed the link on modules provided before? It covers this in the section on packages.

I don’t know what link you mean.

This one-

characterlimit

What is that?
Is it a section?
Can you give me a better link?

I quoted my earlier post, the link is right there in the message.

The text “characterlimit” is simply because the forums have a minimum character limit and quotes don’t count, it’s nonsense and has absolutely nothing to do with the content of the message.

If you don’t like the information contained in that link then have a web search for something that suits you better.