Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'package,': 0.03; 'static': 0.04; 'classes,': 0.05; 'attribute': 0.07; 'class,': 0.07; 'subject:Questions': 0.07; 'alias': 0.09; 'attributes': 0.09; 'function,': 0.09; 'function:': 0.09; 'here?': 0.09; 'line:': 0.09; 'namespace': 0.09; 'obj': 0.09; 'pep': 0.09; 'restriction': 0.09; 'creates': 0.14; '"d"': 0.16; '"from': 0.16; 'assigns': 0.16; 'imports': 0.16; 'lowercase': 0.16; 'subject:import': 0.16; 'xlrd': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'module': 0.19; "skip:' 30": 0.19; 'examples': 0.20; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'form:': 0.24; 'interpret': 0.24; 'module,': 0.24; 'mon,': 0.24; 'file.': 0.24; 'looks': 0.24; 'source': 0.25; 'class.': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'class': 0.32; 'becomes': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'problem.': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'functions.': 0.36; 'in.': 0.36; 'leads': 0.36; 'module.': 0.36; 'words,': 0.36; 'should': 0.36; 'two': 0.37; 'clear': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; '12,': 0.39; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'system.': 0.39; 'either': 0.39; 'called': 0.40; 'skip:x 10': 0.40; 'access,': 0.60; 'subject:"': 0.60; 'simply': 0.61; 'name': 0.63; 'within': 0.65; 'received:74.208': 0.68; 'yourself': 0.78; '(according': 0.84; '...and': 0.84; 'clearer': 0.84; "it'd": 0.84; 'received:74.208.4.194': 0.84; 'ware': 0.91; '2013': 0.98 Date: Mon, 10 Jun 2013 15:10:16 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Questions on "import" and "datetime" References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:nexK39pVhs/0p+uSsOWGF/XF9E320jNRebFaL5uq4xK l/JCSrMAElsgV/5v0MBTaTtMG/LcvzDZaFMK+VHwPPDhuAyw2K rWFgPAIiT3dKc0UVlZXSGXRkLufwtDtHDiLH54xizZrV5VBn9w K/stiAyeWJ42T8OZ+mucMY9WOqnQRWxkIIfcVU4L7q10hX9x1n iyzQ131v8im9SD3nIxCkkZtsG/dxUfBWrw41+ulCbp+Gjx5H+z 3ECjDnjq5LqHMco4UCU5xp7tJNKKKf0rRYU3GCpF0arR9alOe8 syYQlwdFnc1raWonKG4C4a5H7RZShPEWBqu69De0ayVz3+HfQ= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370891438 news.xs4all.nl 15990 [2001:888:2000:d::a6]:47181 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47588 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