import module == from module import *?

后端 未结 3 1975
自闭症患者
自闭症患者 2021-01-16 12:06

I had a problem with the Django tutorial so I asked a question here. No-one knew the answer, but I eventually figured it out with help from Robert. Python seems to be trea

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-16 12:57

    "My question is: Why is datetime in my namespace without me using from datetime import *."

    Because you did import datetime. Then you have datetime in your namespace. Not the CLASS datetime, but the MODULE.

    Python does not treat import datetime the same way as from datetime import *. Stop asking why it does that, when it does not.

    >>> import datetime
    >>> date.today()
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'date' is not defined
    >>> 
    

    There is something else happening. If it's Django magic or not, I don't know. I don't have a Django installation where I can try this at the moment. (If there is a super-quick way of making that happen, tell me. easy_installing Django wasn't enough. :) )

提交回复
热议问题