X-Received: by 10.224.200.202 with SMTP id ex10mr13050495qab.8.1371033672602; Wed, 12 Jun 2013 03:41:12 -0700 (PDT) X-Received: by 10.49.71.173 with SMTP id w13mr395690qeu.21.1371033672574; Wed, 12 Jun 2013 03:41:12 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!bw2no41627qab.0!news-out.google.com!y6ni1854qax.0!nntp.google.com!ch1no4180666qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Wed, 12 Jun 2013 03:41:12 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=159.50.249.150; posting-account=8NHuqgkAAADp22-Nh_kVo1VotZ-yMvjI NNTP-Posting-Host: 159.50.249.150 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Questions on "import" and "datetime" From: Yunfei Dai Injection-Date: Wed, 12 Jun 2013 10:41:12 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.python:47774 On Monday, June 10, 2013 9:10:16 PM UTC+2, Dave Angel wrote: > On 06/10/2013 01:01 PM, Zachary Ware wrote: > > > On Mon, Jun 10, 2013 at 10:37 AM, Yunfei Dai 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. > > > > > > > It's a historical flaw in datetime that the class has the same name as > > the module it's in. It should have been called class Datetime > > (according to pep 10, class names should be capitalized). If that were > > the case, it'd be clear that datetime.Datetime is a class. > > > > It would also be clearer that > > from datetime import Datetime > > > > creates an alias in the present global namespace for the > > datetime.Datetime class, as simply Datetime. > > > > This class has attributes that you can access, like Datetime.hour, and > > it has static methods like Datetime.fromTimeStamp(). > > And like all classes, it's "callable", meaning that you can create an > > instance by pretending it's a function: > > obj = Datetime(2013, 12, 1) > > > > But since it's not a module inside a package, you can't use the form: > > > > import datetime.Datetime > > > > Now, just interpret all the above with a lowercase "D" and the confusion > > becomes clearer. The compiler/interpreter doesn't care either way. > > > > > > > "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. > > > > > > > In other words, since datetime.datetime is a class, not a module, you > > can't just import it. > > > > > > > > > >> 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? > > > > > > > As I said before, datetime.datetime is a class, and the functions within > > it are called methods. > > > > You can see it all for yourself very easily. Use the __file__ attribute > > to locate the source for datetime module on your system. Here's what it > > looks like on mine: > > > > >>> import datetime > > >>> datetime.__file__ > > '/usr/local/lib/python3.3/datetime.py' > > > > Then you can go look at that file. For my copy, the datetime class > > begins at 1301. But you can just search for the following line: > > > > > > > > class datetime(date): > > > > HTH > > > > -- > > DaveA Thank you Dave for your reply! It is very helpful.