Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47582

Re: Questions on "import" and "datetime"

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer02.iad.highwinds-media.com!feed-me.highwinds-media.com!cyclone02.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <zachary.ware@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'package,': 0.03; 'assign': 0.07; 'subject:Questions': 0.07; "'no": 0.09; 'function,': 0.09; 'here?': 0.09; 'namespace': 0.09; 'restriction': 0.09; 'worse': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'assume': 0.14; '"from': 0.16; "'from": 0.16; '(just': 0.16; '(meaning': 0.16; 'advance!': 0.16; 'assigns': 0.16; 'c++)': 0.16; 'importerror': 0.16; 'imports': 0.16; 'new-style': 0.16; 'old-style': 0.16; 'subclass': 0.16; 'subject:import': 0.16; 'xlrd': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'module': 0.19; 'examples': 0.20; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '(since': 0.31; 'class': 0.32; 'maybe': 0.34; 'could': 0.34; 'message.': 0.35; 'problem': 0.35; 'problem.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'functions.': 0.36; 'leads': 0.36; 'module.': 0.36; 'words,': 0.36; 'thanks': 0.36; 'two': 0.37; 'being': 0.38; 'subject:" ': 0.39; 'skip:x 10': 0.40; 'skip:u 10': 0.60; 'subject:"': 0.60; 'hope': 0.61; 'course': 0.61; 'further': 0.61; 'you.': 0.62; 'name': 0.63; 'forward': 0.65; 'to:addr:gmail.com': 0.65; 'due': 0.66; 'here': 0.66; 'between': 0.67; 'believe': 0.68; 'hand': 0.80; 'clearer': 0.84; 'confusing': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=UjnB1wx+2zdTBro6r//QxHf3AjYy/1KlA1mLYydZJ8Y=; b=CzaCiRv6ueGS5Ibh51gIwnUDrTrb9odfADnWakqrSLk2f3XcIgQlVLQvP3yqh/2w2V kSbXjJgM4TMsMTcHoE76i5FzIK9JEOtTq7m0ylbYWzlXPDootd2iFI06tIWcouUm1/Sj ZEkQK2B8XwAjJ8pNOCyYCBNF50ETjoQL06/2lyJwe/lFQBGYhHp1isHrTMcsNA1cjbqr 8lm2zzdeOCjXHxozsCycCoSK7f3MtlhA5XKTqml4SPx74+zufwcnXxK9eknCY7wa9UCR OdqaxLK+SB2MaDOoErNAfMtrrTULBJ1bwXdfh/RYuQjx/WP4PuW3xCXmybNhMGiJHMGN XdRw==
X-Received by 10.204.190.7 with SMTP id dg7mr1683805bkb.52.1370883711203; Mon, 10 Jun 2013 10:01:51 -0700 (PDT)
MIME-Version 1.0
Sender zachary.ware@gmail.com
In-Reply-To <c854d114-bcfa-4491-8a4a-a377998bdf0e@googlegroups.com>
References <c854d114-bcfa-4491-8a4a-a377998bdf0e@googlegroups.com>
From Zachary Ware <zachary.ware+pylist@gmail.com>
Date Mon, 10 Jun 2013 12:01:30 -0500
X-Google-Sender-Auth fBo7nfRmpBiEKYV7DvBYrZF9c0Y
Subject Re: Questions on "import" and "datetime"
To Yunfei Dai <yunfei.dai.sigma@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2974.1370883714.3114.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370883714 news.xs4all.nl 15989 [2001:888:2000:d::a6]:41824
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 6931
Xref csiph.com comp.lang.python:47582

Show key headers only | View raw


On Mon, Jun 10, 2013 at 10:37 AM, Yunfei Dai <yunfei.dai.sigma@gmail.com> wrote:
> Hi all,

Hi Yunfei,

>
> I have some questions on "import":
>
> 1."from datetime import datetime" works well. But I am confused why "import datetime.datetime" leads to importerror. "from xlrd import open_workbook" could be replaced by "from xlrd.open_workbook" without any problem.

I assume you mean "import xlrd.open_workbook" here, as "from
xlrd.open_workbook" would be a SyntaxError :)

> The only difference here is that if "from xlrd import open_workbook" is used we do not have to write "xlrd.open_workbook" in the following code but just "open_workbook". So my understanding of the difference is "from...import..." shortens the code (just like "using namespace std" in C++) but maybe leads to name clash.

"from ... import ..." imports an object from a module and assigns it
to a local name that is the same as the name in the other module.  In
other words, the following two examples do the same thing:

from foo import bar

import foo;bar = foo.bar

If foo.bar happens to be a module (module 'bar' in package 'foo'), you
could also do this:

import foo.bar as bar

...and that restriction is where your problem lies.

> But what is the problem of datetime?

I'm not familiar with xlrd, but I believe the difference between
xlrd.open_workbook and datetime.datetime would be that
xlrd.open_workbook is a module in a package, while datetime.datetime
is a class in a module.  'from ... import ...' can import any object
from the target module/package, and assign it to a local name.
'import ...' on the other hand can only import a module (you'll notice
the ImportError you get when you try 'import datetime.datetime' is 'No
module named datetime'.  This particular example is a bit confusing
due to there being a class in a module of the same name, but try
'import datetime.date' for a clearer message.

> 2.I am also comfused that "datetime.datetime" is a function but whithin "datetime.datetime" there are lots of other functions. So what is the type of "datetime.datetime" on earth? is it a function, or a class or a folder(library) here?

datetime.datetime is actually a type of type 'type' (as can be seen
with 'import datetime;type(datetime.datetime)').  In Python 2, this
means it is a new-style class (meaning it is a subclass of 'object').
In Python 3, it's just a class (since there are no longer old-style
classes).

> Thanks very much in advance! Very looking forward to your answers.
>
> Best,
> Yunfei
>

I hope I have actually answered your question and not just muddied
things further for you.  You can of course ask again if I've made
things worse :)

-- Zach

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Questions on "import" and "datetime" Yunfei Dai <yunfei.dai.sigma@gmail.com> - 2013-06-10 08:37 -0700
  Re: Questions on "import" and "datetime" Zachary Ware <zachary.ware+pylist@gmail.com> - 2013-06-10 12:01 -0500
    Re: Questions on "import" and "datetime" Yunfei Dai <yunfei.dai.sigma@gmail.com> - 2013-06-12 03:29 -0700
  Re: Questions on "import" and "datetime" Dave Angel <davea@davea.name> - 2013-06-10 15:10 -0400
    Re: Questions on "import" and "datetime" Yunfei Dai <yunfei.dai.sigma@gmail.com> - 2013-06-12 03:41 -0700

csiph-web