Resource for getting to know python?

Status
Not open for further replies.

OmniWrench

Ars Scholae Palatinae
1,427
Python and I have had a sorted past. I have been forced to write python code a few times in the past with zero prior experience/exposure, under ultra-strict deadlines and pressure (long story). The python code, well, sucked, and I've developed a bit of a distaste (or perhaps, resentment) for the language as a result.

I think part of the problem is that I am also unfamiliar with Perl. My programming experience is all C, C++ and Java, and it seems like many of the concepts in Python that I'm not used to (like say, duck typing) are not really presented explicitly in resources I've found, they just start dumping out examples and talking about how wonderful it is to be pythonic and I don't really feel like I'm really "getting it". I'm sure some of it is mindset, which I'm working on, but there has to be a better resource than what I've found so far from googling "learn python".

So, I'd like to start fresh. Is there a good entry level book or online resource for learning Python that's well suited for someone with my experience?
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
Duck typing just means that:
  • Type checking is performed at runtime, not compile time.
  • The type checker pays attention to the methods and properties defined on objects instead of its defined type or inheritance relationships.
This means that given this function:
Code:
def f(x):
    print x.foo()
You can pass any object that defines a foo method(), regardless of how it is accomplished, and it will work. C++ templates have similar behavior in many respects, but perform the type checking at compile time.

Being "Pythonic" isn't something to worry about, as there is little consensus and most authors use it as a cover to justify their own way of thinking.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Duck typing just means that:
Type checking is performed at runtime, not compile time.
The type checker pays attention to the methods and properties defined on objects instead of its defined type or inheritance relationships.
This means that given this function:

LH probably meant that in the context of python, I'll just add a bit more.

There's nothing that requires dynamic typing for duck typing in general, there are plenty of languages that will do some degree of "does this named thing seem to exist on the compile time known type, okay use that", sometimes in specific circumstances, it does imply a slightly less rigid type system though (often called weaker, though that statement is horribly abused and I'm not trying to start a debate on it).

If you're dynamic you're pretty much guaranteed to use significant amount of duck typing, but some languages take it to the extreme that was the one of the core 'original' concepts behind OOP (that of message passing[1]) and so many objects will 'accept' messages that you never even knew about at compile time in the first place and can deal with them as they see fit (cased on their own state, not just their type).

1. Many 'OOP' languages make it very clear that when they say "Method" they mean a function attached to a class, rather than just letting the fact that it is often _implemented_ that way be an implementation detail. All your current experience is with those sorts of languages, there may be a certain amount of metal reshuffling when moving to languages that are not quite so rigid.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
ShuggyCoUk":3orkjtho said:
it does imply a slightly less rigid type system though (often called weaker, though that statement is horribly abused and I'm not trying to start a debate on it).
Not really. Strength refers to what conversions will be done for you and what operations are checked. Whether the checker looks at declared types (manifest) or actual operations (structural) is essentially independent.

If you're dynamic you're pretty much guaranteed to use significant amount of duck typing, but some languages take it to the extreme that was the one of the core 'original' concepts behind OOP (that of message passing[1]) and so many objects will 'accept' messages that you never even knew about at compile time in the first place and can deal with them as they see fit (cased on their own state, not just their type).
Which includes Python. Python, for all practical purposes, is a Smalltalk.

1. Many 'OOP' languages make it very clear that when they say "Method" they mean a function attached to a class, rather than just letting the fact that it is often _implemented_ that way be an implementation detail.
As an aside: while it's usually not relevant whether a function is free or a method, it does matter in a few cases in Python.

All your current experience is with those sorts of languages, there may be a certain amount of metal reshuffling when moving to languages that are not quite so rigid.
Generally in Python, what you already know is perfectly valid. What will be new is leveraging the added flexibility afforded to you by certain language features, like __getattr__ / __setattr__ and the like.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Strength refers to what conversions will be done for you and what operations are checked. Whether the checker looks at declared types (manifest) or actual operations (structural) is essentially independent.

It's not totally independent in certain cases.

Case in point c# Linq uses Duck Typing to convert the "from f in foo where blah select f.x" form into compilable code taking extension methods into account, for this it needs to know the types of the things that end up on the left hand side, in this it is allowed to do implicit conversions to achieve it. the trick here is when both are available (with overrides thrown into the mix) which is selected.

I admit this is an edge case (though likely not for the intellisense engine developers and testers) but the 'strength' of the language will directly affect the complexity of the rules for things like this so does have quite an impact.

Haskell apparently can do duck typing but you have to switch monomorphism off for that particular flavour of it (I presume this would be true for most useful ones), so it looks like it impacts other, and very different, languages too.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
ShuggyCoUk":8xw3g7so said:
Case in point c# Linq uses Duck Typing to convert the "from f in foo where blah select f.x" form into compilable code taking extension methods into account, for this it needs to know the types of the things that end up on the left hand side, in this it is allowed to do implicit conversions to achieve it. the trick here is when both are available (with overrides thrown into the mix) which is selected.
It does that at runtime???? Unless it does that, it's not duck typing but rather some form of structural typing. For the most part, the difference is insignificant, but part of the point of duck typing is that the checks are performed "at the last moment".

But I don't see how that expression requires any sort of structural typing in the least. The compiler can infer from the declared types whether a given query transformation is legal or not. You're going to have to provide a better, more specific example. I certainly don't see how allowing (additional) implicit conversions suddenly means the manifest information is ignored.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
but part of the point of duck typing is that the checks are performed "at the last moment".

Ah right, I see where you and I are talking at cross purposes. I view the lateness of binding to be something that heavily influences how duck typing might work in a language, but I don't require it to actually happen at the last moment to be duck typing. Obviously you deem this to be mere structural typing, which is fair enough, I'd say that calling this duck typing is reasonable use of the phrase (and plenty of other people seem to as well) but it's hardly a well defined term[1].

Given that I consider the compile time selection of the method (potentially virtual) based on textual name and signature, rather than on manifest (interface/type) to be duck typing, would you consider this to imply they are not independent in languages where the compiler must chose between: doing method selection on this basis, or on the conventional manifest way?

If you think the whole discussion is moot given the late as possible characteristics you require that's fine, we'll agree to disagree.

Also on a side note, in c# late bind scenarios with dynamic actually do depend on the manifest typing available at that point:
One of the design principles of the C# dynamic feature is that if we have a type that is known at compile time, then at runtime the type analysis honours that. In other words, we only use the runtime type of the things that were actually dynamic; everything else we use the compile-time type.

Thus even if you implemented (in your view) proper "late as possible" Duck Typing via dynamic the manifest typing at compile time would affect this, hence my disputing that they are independent. They *might be* but duck typing is such a lose term meaning different specific things in different languages that I view such an assertion as not actually being that correct or useful.

You may[2] of course be entirely correct within Python.

1. It's a wonderful one though, I take it to mean whatever "walk like a duck and quack like a duck" would imply in the language you are applying the term to
2. and likely are
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
ShuggyCoUk":13kkv6zo said:
I view the lateness of binding to be something that heavily influences how duck typing might work in a language, but I don't require it to actually happen at the last moment to be duck typing.
I don't really care, I just wanted to make sure you understood what separates "duck typing" as it exists in Python from similar systems in statically-typed languages (e.g., C++ templates, Scala). The fact that it is "late as possible" is important in Python, as features like __getattr__ would not work without that property.

Given that I consider the compile time selection of the method (potentially virtual) based on textual name and signature, rather than on manifest (interface/type) to be duck typing, would you consider this to imply they are not independent in languages where the compiler must chose between: doing method selection on this basis, or on the conventional manifest way?
That's the difference between manifest (really nominal, my bad) typing and structural typing, which has little to do with strength. Nominal <-> Structural is one dimension. Strong <-> Weak (Strength) is another. The former determines where/how the compiler determines what operations a type supplies. The latter determines what conversions are allowed and what operations are supported between values of different types.

Anyway, what I'm saying/asking is:
  • Is the LINQ expression actually structural? I didn't think you could put a .Where (extension) method anywhere and it would just work, but I honestly don't know. If you can, it is structural; if you cannot, then it probably is not.
  • Regardless, I still don't see what the answer to that question has to do with the implicit type conversion rules. I didn't even think they were different for LINQ in any regard (are they?)
To show they're interrelated, you need to show that the one couldn't possibly work with out the other.

Thus even if you implemented (in your view) proper "late as possible" Duck Typing via dynamic the manifest typing at compile time would affect this, hence my disputing that they are independent.
If you had "proper" duck typing, then the issue would go away because there wouldn't be any static type checking. Yes, if you intermix static and dynamic types, you have to resolve what operations are done statically, and what operations are done dynamically. Those decisions can change what operations are legal, and when type checking occurs, which is a strength issue. If your static system is nominal and your dynamic system is structural, then obviously you have to resolve the interactions there as well.

But what's important is there's no reason why it has to be this way. Being dynamically typed doesn't require structural typing. Being statically typed doesn't require nominal typing. The conflict here arises solely out of decisions C# chose to make, and not because one decision (e.g., structural typing) requires another (e.g., dynamic typing).
 
Status
Not open for further replies.