Re: [Tutor] Homework Problem

2010-05-29 Thread spir ☣
On Fri, 28 May 2010 19:11:13 -0400
"Shawn Blazer"  wrote:

> 
> This problem told me to use map and filter, so how would I use that to  
> solve it?
[some piece of interactive session] 
> Thanks!

So, where's the problem?

Denis


vit esse estrany ☣

spir.wikidot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list of dicts <-> dict of lists?

2010-05-29 Thread Matthew Wood
On Fri, May 28, 2010 at 6:55 PM, Steven D'Aprano wrote:

> On Fri, 28 May 2010 12:00:46 pm Matthew Wood wrote:
>
> > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values
> > started in python 2.6.  That was a simple mistake.
> >
> > It turns out, that's not the case.  But in general, access to dicts
> > and sets is unordered, so you can't/don't/shouldn't count on
> > ordering.
>
> You can't count on getting a *specific* order, but you can count on
> getting a *consistent* order. (So long as you don't modify the
> dictionary between calls, of course.)
>
>
> > The solution to take keys and values from dict.items()
> > DOES guarantee their ordering, even if dict.keys and dict.values
> > aren't.
>
> And so does zip(d.keys(), d.values()). They both guarantee the same
> consistent ordering.
>
> The fact is, yes, your solution does work, but your rationale for
> preferring it is irrational. That's not meant to be insulting, we all
> have preferences based on irrational little quirks, we wouldn't be
> human otherwise. When there are two equally good solutions, you have to
> pick one or the other for essentially an irrational reason -- if there
> was a rational reason to prefer one over the other, they wouldn't be
> equally good.
>
> If you choose to continue using the dict.items() solution, by all means
> do so because you like it, or because it gives you a warm fuzzy
> feeling, or because it's easier for you to remember. Or because you've
> profiled it and it is 3% faster or uses 1% less memory (I made those
> numbers up, by the way). These are all good reasons for choosing a
> solution over another solution.
>
> But stop trying to justify it on the basis of it being safer and more
> backwards compatible, because that's simply not correct. That's what
> pushes your solution out of personal preference to cargo-cult
> programming: following the form without understanding the semantics.
> The semantics of dict.keys() and values() guarantee the same order.
>
>
> [...]
> > But imagine if that guaranteed behavior started in 2.5 for example.
> > Then, if you want your code to work on 2.3, you'd definitely want to
> > pull them out of the dict via dict.items().
>
> But it didn't start in 2.5. It has been part of Python essentially
> forever. If I argued, "Imagine that dictionaries only gained an items()
> method in 2.5, and you wanted it to work in 2.3, you'd need to avoid
> dict.items()", what would you say?
>
>
> > I think your response was quite rude.
>
> If you can't take constructive criticism without getting offended and
> crying "oh how rude!", there's a serious problem.
>
>
> > I mean really, cargo cult programming?
> > I just tried to suggest a solution and I think it's crappy that you
> > accused me of "programming without understanding what you are doing".
>
> I think it is quite clear that in *this* specific case you don't
> understand what you are doing, because you are recommending a solution
> that you labelled "This line isn't necessary". If it's not necessary,
> why include it?
>
> We all code badly at times. I'm sure if you were to go through my code
> line by line, you'd find some real clangers caused by me failing to
> fully understand what I was doing too. Patches and bug reports are
> welcome :)
>
> If it makes you feel any better, I was once told by Alex Martelli (one
> of the Python demi-gods) that if he were marking my code for an
> assignment he would fail me over what I believed was a trivial
> stylistic difference of opinion. I was declaring globals even if I
> didn't assign to them, e.g.:
>
> def func(x):
>global y
>return x + y
>
> It took me a long time, perhaps a few years, but I've come around to
> Martelli's position on globals and no longer declare them unless I
> assign to them. I still think a fail over such a small issue is awfully
> harsh, but perhaps he was having a bad day. I've come to understand the
> semantics of the global statement better, and can see that unnecessary
> global declarations goes against the purpose and meaning of the
> statement. I was, in short, cargo-cult programming, using global
> without understanding it. As harsh as Martelli's response was, I
> believe I'm a better coder today because of it than if he had just
> patted me on the head and said "that's okay, you write anything you
> like".
>
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


Well, since it appears that my offer to nip the email flame-war in the bud
was declined, I'll reluctantly respond.

And as name dropping Alex Martelli seems the order of the day, I'll go ahead
and share my experience with him as well: He decided to put my code into the
python cook book (for those that demand citations: Python Cookbook, 1st
edition, recipe 2.7, page 49).  He did so while making suggestions on how my
code could be impro

[Tutor] SENTINEL, & more

2010-05-29 Thread spir ☣
Hello,


from the thread: "class methods: using class vars as args?"

On Sat, 29 May 2010 11:01:10 +1000
Steven D'Aprano  wrote:

> On Fri, 28 May 2010 07:42:30 am Alex Hall wrote:
> > Thanks for all the explanations, everyone. This does make sense, and
> > I am now using the
> > if(arg==None): arg=self.arg
> > idea. It only adds a couple lines, and is, if anything, more explicit
> > than what I was doing before.
> 
> You should use "if arg is None" rather than an equality test.
> 
> In this case, you are using None as a sentinel value. That is, you want 
> your test to pass only if you actually receive None as an argument, not 
> merely something that is equal to None.
> 
> Using "arg is None" as the test clearly indicates your intention:
> 
> The value None, and no other value, is the sentinel triggering special 
> behaviour
> 
> while the equality test is potentially subject to false positives, e.g. 
> if somebody calls your code but passes it something like this:
> 
> class EqualsEverything:
> def __eq__(self, other):
> return True
> 
> instead of None.

I'll try to clarify the purpose and use of sentinels with an example. Please, 
advanced programmers correct me. A point is that, in languages like python, 
sentinels are under-used, because everybody tends to une None instead, or as 
all-purpose sentinel.

Imagine you're designing a kind of database of books; with a user interface to 
enter new data. What happens when an author is unknown? A proper way, I guess, 
to cope with this case, is to define a sentinel object, eg:
UNKNOWN_AUTHOR = Object()
There are many ways to define a sentinel; one could have defined "=0" or 
"=False" or whatever. But this choice is simple, clear, and secure because a 
custom object in python will only compare equal to itself -- by default. 
Sentinels are commonly written upercase because they are constant, predefined, 
elements.

Say, when users have to deal with an unknown author, they press a special 
button or enter a special valuen, eg '*', the software then silently converts 
to UNKNOWN_AUTHOR. Now, cases of unknown authors all are *marked* with the same 
mark UNKNOWN_AUTHOR; this mark only happens in this very case, thus only means 
this. In other words, this is a clear & safe *semantic* mark.

Later, when the application operates on data, it can compare the value stored 
in the "author" field, to catch the special mark case UNKNOWN_AUTHOR. Eg

class Book(Object):
...
AUTHOR_DEFAULT_TEXT = ""
def write(self):
...
if self.author is UNKNOWN_AUTHOR:
author_text = Book.AUTHOR_DEFAULT_TEXT
...

Hope I'm clear. In the very case of UNKNOWN_AUTHOR, it would hardly have any 
consequence to use "==", instead of "is", as relational operator for 
comparison. Because, as said above, by default, custom objects only compare 
equal to themselves in python. But
* This default behaviour can be overriden, as shown by Steven above.
* Using "is" clarifies your intent to the reader, including yourself.
* Not all languages make a difference between "==" and "is". (Actually, very 
few do it.) Good habits...



=== additional stuff -- more personal reflexion -- critics welcome ===

Sentinels belong to a wider category of programming elements, or objects, I 
call "marks". (Conventional term for this notion welcome.) Marks are elements 
that play a role in a programmer's model, but have no value. What is the value 
of NOVICE_MODE for a game? of the SPADE card suit? of the character 'ø'? These 
are notions, meaning semantic values, that must exist in an application but 
have no "natural" value -- since they are not values semantically, unlike a 
position or a color.
In C, on could use a preprocessor flag for this:
   #define NOVICE_MODE
   ...
   #ifdef NOVICE_MODE ... #endif
NOVICE_MODE is here like a value-less symbol in the program: precisely what we 
mean. But not all languages have such features. (Indeed, there is a value 
behind the scene, but it is not accessible to the programmer; so, the semantics 
is correct.)

Thus, we need to _arbitrarily_ assign marks values. Commonly, natural numbers 
are used for that: they are called "nominals" (--> 
http://en.wikipedia.org/wiki/Nominal_number) precisely because they act like 
symbol names for things that have no value.
The case of characters is typical: that 'ø' is represented by 248 is just 
arbitrary; we just need something, and software can only deal with values; so, 
we need a value. the only subset of a character set that is not arbitrarily 
ordered is precisely the suite of digits: because they are used to compose 
ordinals, which themselves form the archetype of every order.

In the case of card suits, I could define an independant mark for each suit. 
But the 4 of them also build a whole, namely the set of card suits. For such a 
notion, some languages introduce a builtin feature; for instance Pascal has 
"enumerations" for this 
(http://en.wikipedia.org/wiki/Enumeration

Re: [Tutor] Homework Problem

2010-05-29 Thread Alan Gauld


"Shawn Blazer"  wrote

This problem told me to use map and filter, so how would I use that 
to  solve it?


Because its homework we won't solve it for you, we will only
answer questions or suggest approaches.


From your earlier post it looks like you have all the tools:

recursion, map and filter.

Now what do you not understand? What have you tried?

FWIW
My tutorial covers map and filter in the "Functional Programming"
topic.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SENTINEL, & more

2010-05-29 Thread Lie Ryan
On 05/29/10 18:29, spir ☣ wrote:
> Hello,
> 
> 
> from the thread: "class methods: using class vars as args?"
> 
> On Sat, 29 May 2010 11:01:10 +1000 Steven D'Aprano
>  wrote:
> 
>> On Fri, 28 May 2010 07:42:30 am Alex Hall wrote:
>>> Thanks for all the explanations, everyone. This does make sense,
>>> and I am now using the if(arg==None): arg=self.arg idea. It only
>>> adds a couple lines, and is, if anything, more explicit than what
>>> I was doing before.
>> 
>> You should use "if arg is None" rather than an equality test.
>> 
>> In this case, you are using None as a sentinel value. That is, you
>> want your test to pass only if you actually receive None as an
>> argument, not merely something that is equal to None.
>> 
>> Using "arg is None" as the test clearly indicates your intention:
>> 
>> The value None, and no other value, is the sentinel triggering
>> special behaviour
>> 
>> while the equality test is potentially subject to false positives,
>> e.g. if somebody calls your code but passes it something like
>> this:
>> 
>> class EqualsEverything: def __eq__(self, other): return True
>> 
>> instead of None.
> 
> I'll try to clarify the purpose and use of sentinels with an example.
> Please, advanced programmers correct me. A point is that, in
> languages like python, sentinels are under-used, because everybody
> tends to une None instead, or as all-purpose sentinel.

Sentinels are underused not because everyone uses None, but because in
many cases sentinels can be dangerous if not explicitly checked. In many
cases, python prefers Exceptions (e.g. for-loop iteration) to sentinels.

> Imagine you're designing a kind of database of books; with a user
> interface to enter new data. What happens when an author is unknown?
> A proper way, I guess, to cope with this case, is to define a
> sentinel object, eg: UNKNOWN_AUTHOR = Object() There are many ways to
> define a sentinel; one could have defined "=0" or "=False" or
> whatever. But this choice is simple, clear, and secure because a
> custom object in python will only compare equal to itself -- by
> default. Sentinels are commonly written upercase because they are
> constant, predefined, elements.

In this case, I would prefer an unknown author to be an empty string
(i.e. "") because using object() does not persist between serialization
to the database (not to mention having to special-case it everywhere,
with empty string, you only need to special case whenever you need to).

> Hope I'm clear. In the very case of UNKNOWN_AUTHOR, it would hardly
> have any consequence to use "==", instead of "is", as relational
> operator for comparison. Because, as said above, by default, custom
> objects only compare equal to themselves in python. But * This
> default behaviour can be overriden, as shown by Steven above. * Using
> "is" clarifies your intent to the reader, including yourself. * Not
> all languages make a difference between "==" and "is". (Actually,
> very few do it.) Good habits...
> 
> 
> 
> === additional stuff -- more personal reflexion -- critics welcome
> ===
> 
> Sentinels belong to a wider category of programming elements, or
> objects, I call "marks". (Conventional term for this notion welcome.)
> Marks are elements that play a role in a programmer's model, but have
> no value. What is the value of NOVICE_MODE for a game? of the SPADE
> card suit? of the character 'ø'? These are notions, meaning semantic
> values, that must exist in an application but have no "natural" value
> -- since they are not values semantically, unlike a position or a
> color. 

What *is* "value"? Is there any difference between "semantic value" and
"natural value"? IMHO, there is no difference, "numerical value" is only
a subset of all "value".

> In C, on could use a preprocessor flag for this: #define
> NOVICE_MODE ... #ifdef NOVICE_MODE ... #endif NOVICE_MODE is here
> like a value-less symbol in the program: precisely what we mean. But
> not all languages have such features. (Indeed, there is a value
> behind the scene, but it is not accessible to the programmer; so, the
> semantics is correct.)
> 
> Thus, we need to _arbitrarily_ assign marks values. Commonly, natural
> numbers are used for that: they are called "nominals" (-->
> http://en.wikipedia.org/wiki/Nominal_number) precisely because they
> act like symbol names for things that have no value. The case of
> characters is typical: that 'ø' is represented by 248 is just
> arbitrary; we just need something, and software can only deal with
> values; 

Digital computers can only deal with "natural numbers" (i.e. {0, 1, 2,
3, ...}), that's why we need to encode all values as natural numbers.
integers maps nicely to natural number (0:0, 1:1, -1:2, 2:3, -2:4, 3:5,
-3:6, 4:7, -4:8, ...).

Everything has a value, but the question of whether such value is
representable in a computer is equivalent to asking whether the value is
representable as integers, or in other words, whether the "cardinality"
of the set of all su

[Tutor] class methods as static methods?

2010-05-29 Thread Alex Hall
Hi all,
In Battleship, I have a weapons.py file, currently with just one
missile type (a Harpoon anti-ship missile). This Harpoon class defines
a getImpactCoords method, which returns all coordinates on the map
that it will hit. I would like to not instantiate a Harpoon object,
just call the Harpoon's getImpactCoords method and pass it the
required arguments. Is this possible? Thanks. Sorry if I got the terms
backwards in the subject; I can never remember which is static and
which is non-static.

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as static methods?

2010-05-29 Thread Lie Ryan
On 05/30/10 05:49, Alex Hall wrote:
> Hi all,
> In Battleship, I have a weapons.py file, currently with just one
> missile type (a Harpoon anti-ship missile). This Harpoon class defines
> a getImpactCoords method, which returns all coordinates on the map
> that it will hit. I would like to not instantiate a Harpoon object,
> just call the Harpoon's getImpactCoords method and pass it the
> required arguments. Is this possible? Thanks. Sorry if I got the terms
> backwards in the subject; I can never remember which is static and
> which is non-static.
> 

Yes you can make it a static method or class method:

class Harpoon(object):
@staticmethod
def inst(a, b, c):
print a, b, c
@classmethod
def cmeth(cls, a, b, c):
print cls
print a, b, c

Harpoon.inst(1, 2, 3)
Harpoon.cmeth(1, 2, 3)

the question is, why would you want to? getImpactCoords() doesn't seem
to be a function that makes sense without a missile instance (I may be
mistaken).

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SENTINEL, & more

2010-05-29 Thread Eike Welk
Hey Denis!


On Saturday May 29 2010 10:29:43 spir ☣ wrote:
> I'll try to clarify the purpose and use of sentinels with an example.
>  Please, advanced programmers correct me. A point is that, in languages
>  like python, sentinels are under-used, because everybody tends to une None
>  instead, or as all-purpose sentinel.
> 
> Imagine you're designing a kind of database of books; with a user interface
>  to enter new data. What happens when an author is unknown? A proper way, I
>  guess, to cope with this case, is to define a sentinel object, eg:
>  UNKNOWN_AUTHOR = Object()
> There are many ways to define a sentinel; one could have defined "=0" or
>  "=False" or whatever. But this choice is simple, clear, and secure because
>  a custom object in python will only compare equal to itself -- by default.
>  Sentinels are commonly written upercase because they are constant,
>  predefined, elements.

I waited for a thread like this to appear, because I have a quirky, but IMHO 
elegant, solution for those kinds of variables:

class EnumMeta(type):
def __repr__(self):
return self.__name__

class Enum(object):
__metaclass__ = EnumMeta


Objects are created by inheriting from the Enum class. (Not by instantiating 
it.)

>>> class EAST(Enum): pass
>>> class WEST(Enum): pass
>>> class NORTH(Enum): pass
>>> class SOUTH(Enum): pass


The objects know their name, and when printed their name is printed. In this 
respect they behave similarly to None.

>>> print NORTH, SOUTH, EAST, WEST
NORTH SOUTH EAST WEST


I call the class Enum, but this is certainly the wrong term since the values 
are not enumerated. But is Sentinel the right term for something like this? I 
thought a sentinel is a soldier who guards something. Hello English native 
speakers! What is a good name? 



Eike.

P.S. By the way Denis, an earlier thread from you on the subject got me 
thinking about it. 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as static methods?

2010-05-29 Thread Mark Lawrence

On 29/05/2010 20:49, Alex Hall wrote:

Hi all,
In Battleship, I have a weapons.py file, currently with just one
missile type (a Harpoon anti-ship missile). This Harpoon class defines
a getImpactCoords method, which returns all coordinates on the map
that it will hit. I would like to not instantiate a Harpoon object,
just call the Harpoon's getImpactCoords method and pass it the
required arguments. Is this possible? Thanks. Sorry if I got the terms
backwards in the subject; I can never remember which is static and
which is non-static.



Hi Alex,

See you're still going for it :)

I think that you're trying to build a Yamoto/Musashi before you've built 
a raft from oil drums or whatever :)  If I'm wrong, I'll apologise here 
and now.


For a really great introduction to Python, I suggest diveintopython, 
it's what got me going eight years ago.


Kindest regards.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as static methods?

2010-05-29 Thread Alex Hall
On 5/29/10, Mark Lawrence  wrote:
> On 29/05/2010 20:49, Alex Hall wrote:
>> Hi all,
>> In Battleship, I have a weapons.py file, currently with just one
>> missile type (a Harpoon anti-ship missile). This Harpoon class defines
>> a getImpactCoords method, which returns all coordinates on the map
>> that it will hit. I would like to not instantiate a Harpoon object,
>> just call the Harpoon's getImpactCoords method and pass it the
>> required arguments. Is this possible? Thanks. Sorry if I got the terms
>> backwards in the subject; I can never remember which is static and
>> which is non-static.
>>
>
> Hi Alex,
>
> See you're still going for it :)
>
> I think that you're trying to build a Yamoto/Musashi before you've built
> a raft from oil drums or whatever :)  If I'm wrong, I'll apologise here
> and now.
I have built one app in Python and have experience in Java and
Javascript, as well as some in PHP; in fact, I am going into my fourth
year of college for a computer science degree in September. While they
have not done as much programming as I would like, I have had enough
that I can find the commonalities between languages and generally know
what I am looking for (make this public, turn that into a class
instead of an independent collection of vars...)
That said, I have no professional experience programming and do only
assigned problems and hobby-level programming. My Screenless Widgets
app is nearing beta testing and works to my satisfaction, but I am
sure there is much I could do to improve it. Still, everyone has to
start somewhere...
I say all this not to express any offense at your message - believe
me, none taken - but rather to tell everyone just where I am coming
from.
>
> For a really great introduction to Python, I suggest diveintopython,
> it's what got me going eight years ago.
I feel that I understand the basics; what I am running into are things
that crop up and I learn them as needed; if I learn a concept but then
never use it, I will forget it, or mix it up with a similar comcept in
another language, so I generally attack things by reading intro
tutorials, modifying them, and then continuing from there until I feel
that I can start my own project from scratch and figure out the pieces
as I go along.
>
> Kindest regards.
>
> Mark Lawrence.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as static methods?

2010-05-29 Thread Steven D'Aprano
On Sun, 30 May 2010 05:49:45 am Alex Hall wrote:
> Hi all,
> In Battleship, I have a weapons.py file, currently with just one
> missile type (a Harpoon anti-ship missile). This Harpoon class
> defines a getImpactCoords method, which returns all coordinates on
> the map that it will hit. I would like to not instantiate a Harpoon 
> object, just call the Harpoon's getImpactCoords method and pass it
> the required arguments.

I don't understand the logic here. Surely the impact coordinates depends 
on the individual missile (an instance)?


> Is this possible? Thanks. Sorry if I got the
> terms backwards in the subject; I can never remember which is static
> and which is non-static.


In Python terminology, a static method is an ordinary function that is 
called as a method from either a class or a class instance:

class C(object):
@staticmethod
def method(x):
return "Called with argument %s", % x

Note that the method does not receive an automatic instance argument 
(usually called self) when you call it.

A class method is like an ordinary method, except instead of receiving 
the instance (self) as the first argument, it receives the class 
(usually called cls):

class C(object):
@classmethod
def method(cls, x):
return "Called from class %s with argument %s", % (cls, x)

The method always receives the class, regardless of whether you call it 
from the class using C.method(x) or from an instance C().method(x).

You might also be interested in what I call "dualmethod", which passes 
the class as first argument if you call it from the class, and the 
instance if you call it from the instance:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SENTINEL, & more

2010-05-29 Thread Steven D'Aprano
On Sat, 29 May 2010 06:29:43 pm spir ☣ wrote:

> I'll try to clarify the purpose and use of sentinels with an example.
> Please, advanced programmers correct me. A point is that, in
> languages like python, sentinels are under-used, because everybody
> tends to une None instead, or as all-purpose sentinel.

But None is a perfectly good sentinel, and it is very common! So how can 
you say that sentinels are underused?

Strictly speaking, a sentinel is a guard value in an array, string, list 
or other sequence. See for example:

http://en.wikipedia.org/wiki/Sentinel_value

In this sense, sentinels are rare in Python because sequences usually 
know their own length and so you don't need a special sentinel to mark 
the end of the sequence.

But I extend the term sentinel to mean any special value passed as an 
argument as a signal or mark. In this sense, sentinels are very common, 
and the most common sentinel is None. Other useful sentinels are the 
empty string, 0 and -1, and of course you can create your own unique 
sentinels using object(). 


> Imagine you're designing a kind of database of books; with a user
> interface to enter new data. What happens when an author is unknown?

The obvious way is to use the empty string for unknown or no author. 
This avoids needing any special checks, since the empty string is a 
perfectly good string, and if you need special processing, you can do 
so very simply:

def print_book(record):
if record.title:
print "Title: %s" % record.title
if record.author:
print "Author: %s" % record.author

If you want to distinguish between unknown and no author, the obvious 
solution is to pass "?" as unknown and "" for no author.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as static methods?

2010-05-29 Thread Mark Lawrence

Hi Alex, thanks for the response, please see below.

On 30/05/2010 02:50, Alex Hall wrote:

On 5/29/10, Mark Lawrence  wrote:

On 29/05/2010 20:49, Alex Hall wrote:

Hi all,
In Battleship, I have a weapons.py file, currently with just one
missile type (a Harpoon anti-ship missile). This Harpoon class defines
a getImpactCoords method, which returns all coordinates on the map
that it will hit. I would like to not instantiate a Harpoon object,
just call the Harpoon's getImpactCoords method and pass it the
required arguments. Is this possible? Thanks. Sorry if I got the terms
backwards in the subject; I can never remember which is static and
which is non-static.



Hi Alex,

See you're still going for it :)

I think that you're trying to build a Yamoto/Musashi before you've built
a raft from oil drums or whatever :)  If I'm wrong, I'll apologise here
and now.

I have built one app in Python and have experience in Java and
Javascript, as well as some in PHP; in fact, I am going into my fourth
year of college for a computer science degree in September. While they
have not done as much programming as I would like, I have had enough
that I can find the commonalities between languages and generally know
what I am looking for (make this public, turn that into a class
instead of an independent collection of vars...)
That said, I have no professional experience programming and do only
assigned problems and hobby-level programming. My Screenless Widgets
app is nearing beta testing and works to my satisfaction, but I am
sure there is much I could do to improve it. Still, everyone has to
start somewhere...
I say all this not to express any offense at your message - believe
me, none taken - but rather to tell everyone just where I am coming
from.


I should hope not, I used to be big-headed, but now I'm perfect :)



For a really great introduction to Python, I suggest diveintopython,
it's what got me going eight years ago.

I feel that I understand the basics; what I am running into are things
that crop up and I learn them as needed; if I learn a concept but then
never use it, I will forget it, or mix it up with a similar comcept in
another language, so I generally attack things by reading intro
tutorials, modifying them, and then continuing from there until I feel
that I can start my own project from scratch and figure out the pieces
as I go along.


I suggest that you do *NOT* understand the basics, at least wrt Python, 
otherwise you would not have placed your original queries on c.l.py, 
before being asked to move to this ng/ml.  Regardless of that, you're on 
*THE* finest group of mls/ngs going for getting very sound advice from 
some of the most highly respected guys/gals going.  And if you want some 
kind of feeling of industry experiences, subscribe to the Python 
bugs/development/ideas ngs/mls (if you haven't already done so) and 
you'll very rapidly get an idea of just how difficult software 
development can get.


As an example, look for the thread on comp.lang.python within the last 
24 hours from myself subject "xrange issue 7721".  A relatively simple 
thing you'd have thought, but read the background on the Python bug 
tracker, and you'll see it ain't quite that easy.


But then, what do I know, I've only 34 years industry experience, albeit 
slightly tempered over the last nine years by physical and mental ill 
health.




Kindest regards.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor






I'm now going to bed, as it's 04:08 BST and I'm absolutely shattered.

Kindest regards.

Mark Lawrence.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor