| Thread overview | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 21, 2008 To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Hi, I use to program in Python, but I need some programs to run much faster. So, D seems to be as the best programming language for my needs. Still, there's a long way to go because I've never programmed in C. To begin with, is version 2 just a developer version or should I start by using it? In Python, lists can have variables, functions, text and others as elements. As far as I can recall from reading a C book, arrays in C don't have this possibility. What about it in D? And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D? For example, how could I do something like: valores = [] for c in lista_campos: valores.append(getattr(self,c)), so that I can have all the Is there also any construct similar to dictionaries? Are there D libraries to access PostgreSQL database or do I have to use C's? I don't know about the theoretical issues regarding language development, so documentation with good examples is a must have. Is there something like www.diveintopython.org in the short term horizon? These are just some topics I need to know about D. I'd appreciate some answers. Luis | ||||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Luis P. Mendes | On Mon, 21 Jul 2008 13:51:23 +0200, Luis P. Mendes <luislupeXXXX@gmailxxxx.com> wrote: > To begin with, is version 2 just a developer version or should I start by > using it? D2 is a moving target, so stick with D1 for the moment. > In Python, lists can have variables, functions, text and others as > elements. As far as I can recall from reading a C book, arrays in C > don't have this possibility. What about it in D? You can put all kinds of stuff into arrays, including delegates and classes. D's arrays are a completely different thing than C's arrays, just have a look at the documentation. > And regarding list comprehensions like li = [elem*2 for elem in li]? is > there something close in D? Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: // Please note that this works only if you define the template "each" yourself! [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; }); But I would recommend you concentrate on the basic stuff first. > For example, how could I do something like: > valores = [] > for c in lista_campos: valores.append(getattr(self,c)), so that I can > have all the ? > Is there also any construct similar to dictionaries? Dictionaries are called "Associative arrays" in D. int[char[]] dict; dict["hello"] = 5; > Are there D libraries to access PostgreSQL database or do I have to use > C's? Don't know, but have a look at dsource.org, there are lots of bindings/libraries up. -Mike -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Luis P. Mendes | Luis P. Mendes: Welcome to the D language. I think there are other people around here that come from the Python language. For more questions like these I suggest you the D.learn newsgroup. > I use to program in Python, but I need some programs to run much faster. So, D seems to be as the best programming language for my needs. Most of the times D is faster than CPython, but: - Developing in Python is faster or much faster. - In Python you have more already written libs, and they often are written in C, so you have speed anyway. In D you will probably more often write things by yourself. - D is younger and less used, so you will find 20 times more bugs in the D implementations than in CPython. - In Python there is NumPy, Cython, Psyco, ShedSkin, Inline, Weave and many ways to link external C/Fortran code, so you often don't miss speed. - D too can be used by Python, with Pyd. - Sometimes D code is slower, for example if you use associative arrays (python dicts), and in other situations, so you have to benchmark your code. - If you write a D program the way you write Python, you will often see programs almost as fast as Python ones, or just a bit faster (or sometimes slower). So if you want to go faster you generally must write in a lower-level style. You may need months to understand what this means. - D programs can be quite faster than CPython ones, but if you need speed you may have to use C instead of D, because the D compilers aren't much refined as GCC. I find all the time programs that run 2-3 times faster in C with GCC than in D with DMD (and their source code is exactly the same). When I need more speed I drop to C. - CPython is written in C, so C is much better integrated with Python than D. - If you want to give your code to other people, everyone has a C compiler, while not everyone has a D compiler and knows how to fix bugs in your D code. > Still, there's a long way to go because I've never programmed in C. D language is almost multi-level: you can program it almost as Python, almost as C (low level), or more generally in a middle way. If you need speed you have to program in a low-level style, and at this level D looks more like C, so essentially you will have to learn C. The good thing is that D will allow you to learn it progressively, with a softer learning curve. > To begin with, is version 2 just a developer version or should I start by using it? If you are on Windows I warmly suggest you to learn the D 1.x version, like the DMD compiler V.1.033. Note that the D world is currently in a mess, because the standard library (Phobos) isn't much standard anymore, lot of people (not me yet) are now using an external almost-de-facto standard lib named Tango, that you may want to use in the future (or even right now). I presume lot of people are just waiting for the developers of D to just give up in developing Phobos and switch to Tango as official built-in std lib. > In Python, lists can have variables, functions, text and others as elements. As far as I can recall from reading a C book, arrays in C don't have this possibility. What about it in D? Python lists (that are arrays dynamic on the right) use dynamic typing. In D arrays are statically typed, so you can put only one type inside. There are arrays of variants/boxes that allow you to create arrays with mixed types, but I think they are rarely used in D. CPython isn't slow because it's badly written. Its C sources are probably quite more optimized than the C sources of DMD, and some parts of Cpython come from people like Tim Peters and R. Hettinger and more, that have invented first-class algorithms absent in the D implementation (for example to search strings into other strings, to sort items, to hash items, etc). CPython is slower because it's dynamically typed and because most name access require a hashing, etc. That in summary means that if you want to write faster programs (in D) you have to give up something: like the dynamic typing. > And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D? List comprehensions are food for Python programmers, but they are not present in D, recently I have written a whole post about this: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=73868 So you can: - write a normal loop - use the map/filter of DMD 2.x - use the things you can find in some libraries, like this large one of mine: http://www.fantascienza.net/leonardo/so/libs_d.zip (You may need the "bud" tool to use this in a simple way, because the D language is dumb still being unable to find modules by itself). Or the Tools lib from Downs. Notes: - But you are a newbie of D, so it may be better for you to exercise yourself in the "basic" D language first, before using external libs (and finding if they are good for you). - If you want to write fast programs you probably can't use high/python-like level of coding in the speed-critical parts of your program (but you can use it in all the other parts to shorten your code, and hopefully to put in less bugs). So the Python code: li = [elem*2 for elem in li] In normal D becomes: foreach (ref elem; li) elem *= 2; But that's in-place. To be closer to the Python version you need something like: typeof(li) li2; li2.length = li.length; foreach (i, elem; li) li2[i] = elem * 2; li = li2; (if "li" isn't an array, but something that behaves like an array, then that code may not work, so you may have to keep the "i" counter manually, but this is a very uncommon situation). Or shorter, but possibly slower (I assume li is really a dynamic array and not something else): li = li.dup; foreach (ref elem; li) elem *= 2; With my libs it may become: li = map((BaseType1!(li) elem){ return elem*2; }, li); If you know that the type of "li" is T it becomes: li = map((T elem){ return elem*2; }, li); With the map() of the std.algorithm of the D 2.x the syntax is probably shorter. With Downs' Tools it will have a different syntax. > For example, how could I do something like: > valores = [] > for c in lista_campos: valores.append(getattr(self,c)), so that I can > have all the Python is slow because it's a dynamic language, while D is a static one, so you will have to give up most of the reflection capabilities of Python (expecially if you need running speed and you don't want to write tons of code), like the getattr(). When you post snippets to be translated it's better to post whole little programs that can be run. I presume you point was to show a Python code like this: class Foo: def __init__(self): self.a = 10 self.b = 20 self.c = 30 def all_attributes(self, attributes): return [getattr(self, c) for c in attributes] f = Foo() print f.all_attributes(["b", "c"]) # prints [20, 30] If the names of the attributes are known at compile time then there are short enough ways to do the same thing in D. If they are only known at compile time you will need much more complex solutions, using external libs that use run-time reflection, but it's for experienced users, I presume (in the future the D standard library may add such run-time things, they are possible in Java too). I can show you a solution for the version where names are known at compile time, if you want, but it may require me a bit of coding. In most D programs you don't do those things. > Is there also any construct similar to dictionaries? Thank the Goddess there are, they are named with a quite long but more correct name: "associative arrays" (AAs), they are built-in and you can use them with a simple syntax: int[string] aa; aa["hello".dup] = 10; etc. Note that in many situations Python dicts are faster or much faster than D associative arrays. So in speed-critical points of your code you may have to invent creative ways to avoid using them, like using arrays of bools to represent the set, using enums, using sorted arrays with a binary search, etc. Often what's slow in D is fast in Python and vice versa (but very often what's fast in D is fast in Psyco too). I leave your other questions to other people. Bye, bearophile | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | "bearophile" <bearophileHUGS@lycos.com> wrote in message news:g621m4$2j6a$1@digitalmars.com... > - CPython is written in C, so C is much better integrated with Python than D. Uh -- Pyd? > int[string] aa; > aa["hello".dup] = 10; > etc. I don't know why you're .dupping there. > Note that in many situations Python dicts are faster or much faster than D associative arrays. So in speed-critical points of your code you may have to invent creative ways to avoid using them, like using arrays of bools to represent the set, using enums, using sorted arrays with a binary search, etc. Often what's slow in D is fast in Python and vice versa (but very often what's fast in D is fast in Psyco too). Or, you know, use another hash implementation. Like Tango's HashMap, which is consistently faster than the built-in AAs. Or write your own. | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley: bearophile: > > - CPython is written in C, so C is much better integrated with Python than D. > Uh -- Pyd? My post refers to Pyd too, do a Find on it. But what I have written there is correct, CPython is written in C, and you have Boost.Python, Weave, Python Inline, ShedSkink, Pyrex, Cython, Swig, etc etc etc. I like Pyd, but for C you can find 20 more things to use it with/through/along Python. > > int[string] aa; > > aa["hello".dup] = 10; > > etc. > > I don't know why you're .dupping there. Maybe it's useless, sorry. But in D V.1.x the management of literal strings is awful, so I often dup them to avoid nasty bugs later. In the past I have described in this newsgroup two different kinds of bugs derived from string literals, that was mostly ignored. In D 2.x strings are immutable so many of those problems may be solved if you use "string". > Or, you know, use another hash implementation. I presume the OP was asking about a built-in one, because you can use a hash_map in C++ too (even if it may sometimes be slower than Python dicts). So far I haven't seen external and reliable hash libs that can be used with Phobos that are fast enough. Tango ones too aren't fast enough, I think. > Like Tango's HashMap, which is consistently faster than the built-in AAs. They are just a bit faster, their syntax is much worse (and I am using Phobos, but that's not a fault of Tango). Note that I am not saying this to bash D or Tango, I know D is a young language still, I was just stating a fact regarding how I now see things. > Or write your own. A really good hash implementation may take few weeks to be written and some years to be tuned/refined :-) If you take a look at the Lua language, you can see that they have recently found ways to speed up their associative arrays significantly. You can't expect a D newbie coming from Python and not knowing C to write a first-class hash implementation. Probably only few (2? 5?) people here are able to do it, and I am not among them :-) Bye, bearophile | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike | Hi Mike, Mon, 21 Jul 2008 15:02:17 +0200, Mike wrote: > On Mon, 21 Jul 2008 13:51:23 +0200, Luis P. Mendes <luislupeXXXX@gmailxxxx.com> wrote: > >> To begin with, is version 2 just a developer version or should I start by using it? > > D2 is a moving target, so stick with D1 for the moment. > >> In Python, lists can have variables, functions, text and others as elements. As far as I can recall from reading a C book, arrays in C don't have this possibility. What about it in D? > > You can put all kinds of stuff into arrays, including delegates and classes. D's arrays are a completely different thing than C's arrays, just have a look at the documentation. Ok. > >> And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D? > > Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: > > // Please note that this works only if you define the template "each" > yourself! > [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; > }); > > But I would recommend you concentrate on the basic stuff first. By something close I meant some kind of (high-level) construct that D would have to offer. As in this example, as in other I wrote I didn't want to mimic Python. > >> For example, how could I do something like: valores = [] >> for c in lista_campos: valores.append(getattr(self,c)), so that I can >> have all the > > ? Sorry for the paste error. I wanted to know if I can load values for attributes in a concise manner. For example, right now I'm working on a project that reads and writes circa 40 values for each instance of an object and in Python I'm able to load the values on a for loop as mentioned above. Thank you for your time and comments. Luis | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | Hello bearophile, thank you for your time. Please see comments below. Mon, 21 Jul 2008 09:07:16 -0400, bearophile wrote: > Luis P. Mendes: > > Welcome to the D language. I think there are other people around here that come from the Python language. For more questions like these I suggest you the D.learn newsgroup. Ok. > >> I use to program in Python, but I need some programs to run much faster. So, D seems to be as the best programming language for my needs. > > Most of the times D is faster than CPython, but: - Developing in Python is faster or much faster. - In Python you have more already written libs, and they often are written in C, so you have speed anyway. In D you will probably more often write things by yourself. - D is younger and less used, so you will find 20 times more bugs in the D implementations than in CPython. - In Python there is NumPy, Cython, Psyco, ShedSkin, Inline, Weave and many ways to link external C/Fortran code, so you often don't miss speed. I use Psyco to speed up. - D too can be used by Python, with > Pyd. - Sometimes D code is slower, for example if you use associative arrays (python dicts), and in other situations, so you have to benchmark your code. - If you write a D program the way you write Python, you will often see programs almost as fast as Python ones, or just a bit faster (or sometimes slower). So if you want to go faster you generally must write in a lower-level style. Ok. >> Still, there's a long way to go because I've never programmed in C. > > D language is almost multi-level: you can program it almost as Python, almost as C (low level), or more generally in a middle way. If you need speed you have to program in a low-level style, and at this level D looks more like C, so essentially you will have to learn C. The good thing is that D will allow you to learn it progressively, with a softer learning curve. As D promises to be almost as fast as C and to have some goodies of Python, this was what I needed to know. >> To begin with, is version 2 just a developer version or should I start by using it? > > If you are on Windows I warmly suggest you to learn the D 1.x version, like the DMD compiler V.1.033. I use exclusively Linux. Should I stick with version 1 still? >> In Python, lists can have variables, functions, text and others as elements. As far as I can recall from reading a C book, arrays in C don't have this possibility. What about it in D? > > Python lists (that are arrays dynamic on the right) use dynamic typing. In D arrays are statically typed, so you can put only one type inside. There are arrays of variants/boxes that allow you to create arrays with mixed types, but I think they are rarely used in D. So can I define something as this kind of list: [type char, type int, type float,...] ? > CPython is slower because it's dynamically typed and > because most name access require a hashing, etc. That in summary means > that if you want to write faster programs (in D) you have to give up > something: like the dynamic typing. I know that static typing provides a faster execution and it's something that I could use in Python if it provided that possibility. The trade-off between dynamic typing and speed, for me, is in favor of speed. Type safety is also another advantage for me. >> And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D? > > List comprehensions are food for Python programmers, but they are not present in D, recently I have written a whole post about this: http://www.digitalmars.com/webnews/newsgroups.php? art_group=digitalmars.D&article_id=73868 > > So you can: > - write a normal loop > - use the map/filter of DMD 2.x > - use the things you can find in some libraries, like this large one of > mine: http://www.fantascienza.net/leonardo/so/libs_d.zip (You may need > the "bud" tool to use this in a simple way, because the D language is > dumb still being unable to find modules by itself). Or the Tools lib > from Downs. > > Notes: > - But you are a newbie of D, so it may be better for you to exercise > yourself in the "basic" D language first, before using external libs > (and finding if they are good for you). - If you want to write fast > programs you probably can't use high/python-like level of coding in the > speed-critical parts of your program (but you can use it in all the > other parts to shorten your code, and hopefully to put in less bugs). This is what I was expecting from D. > When you post snippets to be translated it's better to post whole little programs that can be run. I presume you point was to show a Python code like this: > > class Foo: > def __init__(self): > self.a = 10 > self.b = 20 > self.c = 30 > def all_attributes(self, attributes): > return [getattr(self, c) for c in attributes] > f = Foo() > print f.all_attributes(["b", "c"]) > # prints [20, 30] > > If the names of the attributes are known at compile time then there are short enough ways to do the same thing in D. If they are only known at compile time you will need much more complex solutions, using external libs that use run-time reflection, but it's for experienced users, I presume (in the future the D standard library may add such run-time things, they are possible in Java too). The situations I've used getattr, names of the attributes were all known and defined in the script. >> Is there also any construct similar to dictionaries? > > Thank the Goddess there are, they are named with a quite long but more correct name: "associative arrays" (AAs), they are built-in and you can use them with a simple syntax: > > int[string] aa; > aa["hello".dup] = 10; > etc. > > Note that in many situations Python dicts are faster or much faster than D associative arrays. So in speed-critical points of your code you may have to invent creative ways to avoid using them, like using arrays of bools to represent the set, using enums, using sorted arrays with a binary search, etc. Often what's slow in D is fast in Python and vice versa (but very often what's fast in D is fast in Psyco too). In Python too, I can have other constructs that replace dicionaries and are probably faster, but less intuitive. But my point was to know if D had such kind of tool and if it was much faster. Thank you again bearophile | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Luis P. Mendes | Luis P. Mendes Wrote:
> >> And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D?
> >
> > Depends on how you define close. There is foreach and with some template trickery you can get similar syntax:
> >
> > // Please note that this works only if you define the template "each"
> > yourself!
> > [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline;
> > });
> >
> > But I would recommend you concentrate on the basic stuff first.
> By something close I meant some kind of (high-level) construct that D would have to offer. As in this example, as in other I wrote I didn't want to mimic Python.
I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like:
foreach(index, elem; li)
li[index] = elem*2;
Note that I may have reversed the order of index and elem. I never remember which one should come first...
| |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Luis P. Mendes | "Luis P. Mendes" <luislupeXXXX@gmailXXXX.com> wrote in message news:g62b67$299b$3@digitalmars.com... >> If you are on Windows I warmly suggest you to learn the D 1.x version, like the DMD compiler V.1.033. > I use exclusively Linux. Should I stick with version 1 still? I'm not sure what connection bearophile was making between what OS you use and what version of D to use. D2 is in alpha, no matter what OS you're using, and most/all libraries are written for D1, so you're probably better off using D1 unless you really, really want some of the new features in D2. > So can I define something as this kind of list: > [type char, type int, type float,...] ? No. There are structures and classes if you want to group multiple different types, as well as tuples, but they are not first-class objects as in Python and are strictly compile-time entities. It's not that lists of varying types aren't useful, it's that the way you'd do it in D would be (possibly very) different from the way you'd do it in Python, is all. Syntax is sugar. It's not enough to say that "language X doesn't have feature Y so it's not possible to do algorithm Z." It probably is possible, just not in the same way as if you had feature Y. | |||
July 21, 2008 Re: To begin in D coming from Python | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jason House | "Jason House" <jason.james.house@gmail.com> wrote in message news:g62d9m$crt$1@digitalmars.com... > Luis P. Mendes Wrote: >> >> And regarding list comprehensions like li = [elem*2 for elem in li]? is there something close in D? >> > >> > Depends on how you define close. There is foreach and with some >> > template >> > trickery you can get similar syntax: >> > >> > // Please note that this works only if you define the template "each" >> > yourself! >> > [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; >> > }); >> > >> > But I would recommend you concentrate on the basic stuff first. >> By something close I meant some kind of (high-level) construct that D would have to offer. As in this example, as in other I wrote I didn't want to mimic Python. > > I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like: > > foreach(index, elem; li) > li[index] = elem*2; foreach(ref elem; li) elem *= 2; | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply