Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47574 > unrolled thread
| Started by | Yunfei Dai <yunfei.dai.sigma@gmail.com> |
|---|---|
| First post | 2013-06-10 08:37 -0700 |
| Last post | 2013-06-12 03:41 -0700 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Yunfei Dai <yunfei.dai.sigma@gmail.com> |
|---|---|
| Date | 2013-06-10 08:37 -0700 |
| Subject | Questions on "import" and "datetime" |
| Message-ID | <c854d114-bcfa-4491-8a4a-a377998bdf0e@googlegroups.com> |
Hi all, 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. 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. But what is the problem of datetime? 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? Thanks very much in advance! Very looking forward to your answers. Best, Yunfei
[toc] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2013-06-10 12:01 -0500 |
| Message-ID | <mailman.2974.1370883714.3114.python-list@python.org> |
| In reply to | #47574 |
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
[toc] | [prev] | [next] | [standalone]
| From | Yunfei Dai <yunfei.dai.sigma@gmail.com> |
|---|---|
| Date | 2013-06-12 03:29 -0700 |
| Message-ID | <98160f88-01dc-434b-804f-44f102851db6@googlegroups.com> |
| In reply to | #47582 |
On Monday, June 10, 2013 7:01:30 PM UTC+2, Zachary Ware wrote: > 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 Hi Zach, Thanks so much for your quick, long and detailed reply and sorry for replying you late. It is really helpful for me understanding "import". Yunfei
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-06-10 15:10 -0400 |
| Message-ID | <mailman.2978.1370891438.3114.python-list@python.org> |
| In reply to | #47574 |
On 06/10/2013 01:01 PM, Zachary Ware wrote:
> 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.
>
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.
<SNIP>
> "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.
<SNIP>
>
>> 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
[toc] | [prev] | [next] | [standalone]
| From | Yunfei Dai <yunfei.dai.sigma@gmail.com> |
|---|---|
| Date | 2013-06-12 03:41 -0700 |
| Message-ID | <f91dbdc5-b8a3-420c-ab67-336bb74c2e10@googlegroups.com> |
| In reply to | #47588 |
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 <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. > > > > > > > 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. > > > > <SNIP> > > > "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. > > > > <SNIP> > > > > > >> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web