Question mark in variable and function names

Discussion in 'Python' started by Andr? N?ss, Oct 6, 2004.

  1. Andr? N?ss

    Andr? N?ss Guest

    One thing I liked about Lisp was the ability to use the question mark
    (and the exclamation mark) in function names. I found this
    particularily useful when checking boolean properties of a object like
    for example myObj.isContextSet. It just feels so much more natural to
    write myObj.contextSet?

    I also found it neat that destructive operations were clearly marked
    with !.

    Is there anything preventing this from being possible in Python?

    One of the things I really love about Python is the way it feels when
    I type it. It just comes completely natural to write "for foo in bar"
    and then push the colon, all those curly braces on the other hand
    always feel awkward.
    Andr? N?ss, Oct 6, 2004
    #1
    1. Advertising

  2. Andr? N?ss wrote:
    > One thing I liked about Lisp was the ability to use the question mark
    > (and the exclamation mark) in function names. I found this
    > particularily useful when checking boolean properties of a object like
    > for example myObj.isContextSet. It just feels so much more natural to
    > write myObj.contextSet?
    >
    > I also found it neat that destructive operations were clearly marked
    > with !.
    >
    > Is there anything preventing this from being possible in Python?


    That will make Python more similar to Ruby.

    --George
    George Yoshida, Oct 6, 2004
    #2
    1. Advertising

  3. Andr? N?ss wrote:

    > One thing I liked about Lisp was the ability to use the question mark
    > (and the exclamation mark) in function names. I found this
    > particularily useful when checking boolean properties of a object like
    > for example myObj.isContextSet. It just feels so much more natural to
    > write myObj.contextSet?
    >
    > I also found it neat that destructive operations were clearly marked
    > with !.
    >
    > Is there anything preventing this from being possible in Python?


    The fact that Guido has different aesthetic preferences than you do? And
    personally I would call it obj.is_context_set() so we have different
    preferences too <wink>.

    Actually, the "Pythonic" way of doing it might be to do a try/except
    block that assumes it's a context set. Another alternative is to just
    have an attribute called context_set, which could actually be a
    descriptor that calls another function...

    > One of the things I really love about Python is the way it feels when
    > I type it. It just comes completely natural to write "for foo in bar"
    > and then push the colon, all those curly braces on the other hand
    > always feel awkward.


    Me too.
    --
    Michael Hoffman
    Michael Hoffman, Oct 6, 2004
    #3
  4. Andr? N?ss <> wrote:

    > One thing I liked about Lisp was the ability to use the question mark
    > (and the exclamation mark) in function names. I found this
    > particularily useful when checking boolean properties of a object like
    > for example myObj.isContextSet. It just feels so much more natural to
    > write myObj.contextSet?


    Even though I haven't used Lisp extensively, I agree with you. Naming
    all predicates with a trailing question mark, and:

    > I also found it neat that destructive operations were clearly marked
    > with !.


    ops which modify their arguments with a trailing bang, IS a great idea
    for readability. (I hope this shows that one's preferences are NOT
    entirely determined by what one's used to: e.g., even though I speak
    Italian and not Spanish, I think Spanish has two great ideas which
    Italian should adopt [not one chance in a zillion that it will, sigh]:
    start questions and exclamations with reversed versions of
    questionmarks, and bangs, respectively; and, mark down ALL the accents
    that don't fall in the default syllable...).


    > Is there anything preventing this from being possible in Python?


    Nothing except Guido's decisions in the matter. As this would cause no
    backwards incompatibility, it could even be introduced in Python 2.5,
    _IF_ Guido could be convinced (of course, for backwards compatibility, a
    zillion existing methods in built-ins and the Standard Library would
    still be spelled like they are today -- unfortunate but inevitable; at
    best you could add SYNONYMS with trailing qmarks and bangs, alas).

    Ruby has this convention and it seems to work wonderfully well for them.
    I wish Python could grow it, too, but I don't think I stand any chance
    of convincing Guido -- perhaps somebody else can.


    Alex
    Alex Martelli, Oct 6, 2004
    #4
  5. On Wed, Oct 06, 2004 at 07:11:02PM +0200, Alex Martelli wrote:

    > Ruby has this convention and it seems to work wonderfully well for them.
    > I wish Python could grow it, too, but I don't think I stand any chance
    > of convincing Guido -- perhaps somebody else can.


    For my money this is *the* biggest syntactic wart in Python. I think ?
    and ! at the ends of function/method names is an insanely good idea
    that Python should steal. I'm not holding my breath, but it would be
    really great to at least have a community conversation about this.

    Best,
    Kendall Clark
    Managing Editor, XML.com
    Kendall Clark, Oct 6, 2004
    #5
  6. Alex Martelli wrote:

    > Andr? N?ss <> wrote:
    >
    >> One thing I liked about Lisp was the ability to use the question mark
    >> (and the exclamation mark) in function names. I found this
    >> particularily useful when checking boolean properties of a object like
    >> for example myObj.isContextSet. It just feels so much more natural to
    >> write myObj.contextSet?

    >
    > Even though I haven't used Lisp extensively, I agree with you. Naming
    > all predicates with a trailing question mark, and:
    >
    >> I also found it neat that destructive operations were clearly marked
    >> with !.

    >
    > ops which modify their arguments with a trailing bang, IS a great idea
    > for readability. (I hope this shows that one's preferences are NOT


    Well, there would go the two remaining special, useful chars in ipython:

    In [3]: x=1

    In [4]: x?
    Type: int
    Base Class: <type 'int'>
    String Form: 1
    Namespace: Interactive
    Docstring:
    int(x[, base]) -> integer

    Convert a string or number to an integer, if possible. A floating point
    argument will be truncated towards zero (this does not include a string
    representation of a floating point number!) When converting a string, use
    the optional base. It is an error to supply a base when converting a
    non-string.


    In [5]: !uname -a
    Linux haar.colorado.edu 2.4.22-1.2199.nptl #1 Wed Aug 4 12:21:48 EDT 2004 i686
    i686 i386 GNU/Linux


    Since I just finished replacing all uses of @ for ipython's magic functions,
    I'm really not too keen on this idea. And given that there is a lot of
    precedent for !cmd meaning 'escape to shell' in several environments (gnuplot,
    matlab), and for foo? as 'info about foo' (Mathematica), I hope I'd have a
    stronger case of opposing these changes if they ever came to Guido.

    I did NOT oppose the @decorator one strongly, as I felt that ipython could just
    adapt if there was consensus on the benefit of @ for the language, and that
    the loss to ipython was near-zero (just a few hours of my time changing code
    and docs). The change was made, and current CVS ipytho uses %magic instead,
    which is fine.

    But if something like this (!?) were to be seriously considered, I would most
    definitely try to oppose it. Granted, my opinion is just one amongst many.
    But the fact that ipython is proving to be a growingly popular tool for
    interactive work in multiple contexts, and that this popularity is in no small
    part due precisely to the utility of features like !cmd and foo?, would
    hopefully give my humble opinion the weight of a larger community.

    That, and the fact that in _my_ opinion, names-with-embedded-punctuation look
    ugly :)

    Best,

    f
    Fernando Perez, Oct 6, 2004
    #6
  7. Andr? N?ss

    Dave Pawson Guest

    On Wed, 2004-10-06 at 18:11, Alex Martelli wrote:
    > Andr? N?ss <> wrote:
    >
    > > One thing I liked about Lisp was the ability to use the question mark
    > > (and the exclamation mark) in function names. I found this
    > > particularily useful when checking boolean properties of a object like
    > > for example myObj.isContextSet. It just feels so much more natural to
    > > write myObj.contextSet?

    >
    > Even though I haven't used Lisp extensively, I agree with you. Naming
    > all predicates with a trailing question mark,


    Makes for a nice readability feature, though lisp does have lots of
    inbuilt function, with functionp being the predicate form.
    oddp
    evenp
    exp ... oops :)

    Yes, I like the Scheme idea of a ? suffix being the predicate :)


    --
    Regards DaveP.
    XSLT&Docbook FAQ
    http://www.dpawson.co.uk/xsl
    Dave Pawson, Oct 6, 2004
    #7
  8. Andr? N?ss

    Andrew Dalke Guest

    Alex Martelli:
    > As this would cause no
    > backwards incompatibility, it could even be introduced in Python 2.5,


    or!=0

    is currently valid. Changing the tokenizer to allow
    trailing ? and ! would turn this into an assignment to
    "or!". A quick grep finds these two places in the standard
    lib that would become a syntax error

    robotparser.py: if a!=b:


    threading.py: self._note("%s.notify(): notifying %d waiter%s", self, n,
    threading.py: n!=1 and "s" or "")

    Had we added C's "?:" ternary operator we likely would have
    had similar small problems there. Good thing we didn't do that. :)

    Andrew
    Andrew Dalke, Oct 6, 2004
    #8
  9. Alex Martelli wrote:

    > Ruby has this convention and it seems to work wonderfully well for them.


    I have used Ruby before switching to Python and I found
    the ! symbol to be quite unhelpful. I don't recall the
    exact syntax properly, what I remember is having something like

    line.strip()

    returning the stripped line while

    line.strip!()

    stripping the line in place.

    What this meant that there were a bunch of methods now
    with two distinct behaviors. The possibilities to foul up just
    doubled. Basically I found myself sticking to one behavior
    and using that consistently so that I did not have to
    make an extra effort to double check the symbol at the end.

    I find it much simpler if there is only one behavior, it either mutates
    the input or it doesn't, one that makes the most sense
    (as it is usually with python).

    Istvan.
    Istvan Albert, Oct 6, 2004
    #9
  10. Andr? N?ss

    Harry George Guest

    (Andr? N?ss) writes:

    > One thing I liked about Lisp was the ability to use the question mark
    > (and the exclamation mark) in function names. I found this
    > particularily useful when checking boolean properties of a object like
    > for example myObj.isContextSet. It just feels so much more natural to
    > write myObj.contextSet?
    >
    > I also found it neat that destructive operations were clearly marked
    > with !.
    >
    > Is there anything preventing this from being possible in Python?
    >
    > One of the things I really love about Python is the way it feels when
    > I type it. It just comes completely natural to write "for foo in bar"
    > and then push the colon, all those curly braces on the other hand
    > always feel awkward.


    I had the impression that "!" and "?" were more Scheme-isms than
    Common-Lisp-isms.

    For "?", a typical CL idiom is "p" suffix, without or without leading
    "-". For clarity, I always use the "-p" form. In Python this becomes
    "_p", as in "ready_for_data_p". I do this for boolean (True/False)
    variables, methods, and functions.

    For "!", a typical CL idiom is "n" prefix (as in "nreverse"). I've
    never tried doing a Python equivalent, because I only see destructive
    operations in builtins (which are already named).

    What prevents using "?" and "!"? Hopefully a graceful and poetic
    sense of balance on the part of language designers. In the extreme
    we'd have APL. Partway there, we'd have Ruby and perl. I personally
    like Python the way it is.




    --

    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 342-0007
    Harry George, Oct 6, 2004
    #10
  11. Kendall Clark <> wrote:

    > On Wed, Oct 06, 2004 at 07:11:02PM +0200, Alex Martelli wrote:
    >
    > > Ruby has this convention and it seems to work wonderfully well for them.
    > > I wish Python could grow it, too, but I don't think I stand any chance
    > > of convincing Guido -- perhaps somebody else can.

    >
    > For my money this is *the* biggest syntactic wart in Python. I think ?


    I don't think of this as a wart, personally -- just a little _lack_.

    > and ! at the ends of function/method names is an insanely good idea
    > that Python should steal. I'm not holding my breath, but it would be
    > really great to at least have a community conversation about this.


    Sure, we should make a PEP if we have any real hope of it ever really
    happening...


    Alex
    Alex Martelli, Oct 6, 2004
    #11
  12. Fernando Perez <> wrote:
    ...
    > precedent for !cmd meaning 'escape to shell' in several environments (gnuplot,
    > matlab), and for foo? as 'info about foo' (Mathematica), I hope I'd have a
    > stronger case of opposing these changes if they ever came to Guido.


    What about a compromise: allow ! and ? only as the LAST (and NOT only)
    character of an identifier. This leaves you the !cmd as escape to
    shell, and you only need to ask users to leave a space before the
    question mark (as in 'x ?') when they're questing for info about x (or
    you could switch to ?x -- whatever...).

    > But if something like this (!?) were to be seriously considered, I would most
    > definitely try to oppose it. Granted, my opinion is just one amongst many.
    > But the fact that ipython is proving to be a growingly popular tool for
    > interactive work in multiple contexts, and that this popularity is in no small
    > part due precisely to the utility of features like !cmd and foo?, would
    > hopefully give my humble opinion the weight of a larger community.


    I love iPython, but the (hypothetical) moment iPython or any other tool
    starts holding back the development of the _language_ is the moment I
    turn into a deadly enemy of the tool in question, _whatever_ it may be.
    We have enough tails-wagging-dogs in the real world, we don't need more
    of that in the fairytale world of the Snake...!-)

    > That, and the fact that in _my_ opinion, names-with-embedded-punctuation look
    > ugly :)


    Embedded, yes, trailing, no -- that's the compromise I'm proposing... no
    'foo!bar' or 'fee?fie' identifier, just _trailing_ bangs and qmarks...


    Alex
    Alex Martelli, Oct 6, 2004
    #12
  13. Fernando Perez <> writes:

    >> Andr? N?ss <> wrote:
    >>
    >>> One thing I liked about Lisp was the ability to use the question mark
    >>> (and the exclamation mark) in function names.

    >
    > Well, there would go the two remaining special, useful chars in ipython:
    >
    > In [4]: x?

    ....
    > In [5]: !uname -a

    ....

    If python only allowed ! and ? at the end of identifiers, then !uname
    would be fine, and ?x could be used in place of x?

    But the problem mentioned in other post with code like x!=0 is more
    serious.

    Dan
    Dan Christensen, Oct 6, 2004
    #13
  14. In article <1gl90yk.v91yz8w1z0b2N%>,
    (Alex Martelli) wrote:

    > Andr? N?ss <> wrote:
    >
    > > One thing I liked about Lisp was the ability to use the question mark
    > > (and the exclamation mark) in function names. [...]

    >
    > Even though I haven't used Lisp extensively, I agree with you. [...]
    >
    > ops which modify their arguments with a trailing bang, IS a great idea
    > for readability. [...]
    >
    > Ruby has this convention and it seems to work wonderfully well for them.
    > I wish Python could grow it, too, but I don't think I stand any chance
    > of convincing Guido -- perhaps somebody else can.


    I also like this convention in Scheme, but I don't like the way it looks
    in an infix-laden language like Python.

    Whereas:
    (predicate? arg1 arg2 ... argn)

    .... looks good and legible to me, I find:

    predicate?(arg1, arg2, ..., argn)

    .... far less appealing, because of the juxtaposition of ?(.

    I realize this is purely a personal aesthetic distaste, but Python is
    generally quite a pretty language, and I'm afraid this addition, as
    sensible as it is, might work against that for comparatively little
    benefit.

    Cheers,
    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
    Michael J. Fromberger, Oct 6, 2004
    #14
  15. Alex Martelli wrote:

    > I love iPython, but the (hypothetical) moment iPython or any other tool
    > starts holding back the development of the _language_ is the moment I
    > turn into a deadly enemy of the tool in question, _whatever_ it may be.
    > We have enough tails-wagging-dogs in the real world, we don't need more
    > of that in the fairytale world of the Snake...!-)


    I've certainly always held the opinion that ipython should in _all_ respects
    adapt to whatever directions the language moves in. In fact, I've in the past
    rejected proposals for changing ipython in directions which would make it
    incompatible with python's defaults, because I feel that ipython should always
    be a python _tool_, hence subservient to the language.

    So I'm NOT advocating 'holding back the development of the language' because of
    ipython (or any other tool, for that matter), as I ultimately share your
    position. I simply think that Python is more than a syntactic specification,
    it's also a set of tools, libraries and community, and I think much of what
    makes python attractive is the combined power of all those things. Just like
    the needs of one community have in the past been taken into account when
    developing the language (extended slicing originated for Numeric), I hope this
    will remain true in the future.

    I would NEVER try to hold back Python's development (I'm already considered a
    python nut evangelist by many :). However, I would certainly voice my concerns
    in the hope that a good compromise could be reached, which could satisfy the
    language while harming other tools (like ipython in this case) as little as
    possible.

    And for the record, yes, the ?!-at-the-end-only would qualify as such a
    minimal-impact compromise in my book :) The ! would be a non-issue, and users
    could type a space before the ?. And on ?foo, I get to use my time machine
    today :)

    In [1]: x=1

    In [2]: ?x
    Type: int
    Base Class: <type 'int'>
    String Form: 1
    Namespace: Interactive
    Docstring:
    int(x[, base]) -> integer

    ....

    Note that I still don't _like_ the ?!-in-names, but that's a different matter,
    since now we're just talking about personal taste :)

    Best,

    f
    Fernando Perez, Oct 6, 2004
    #15
  16. (Andr? N?ss) writes:

    > One thing I liked about Lisp was the ability to use the question mark
    > (and the exclamation mark) in function names.


    I wrote a patch for this once. It's pretty easy.

    I'd also like for '-' to be a allowable character in identifiers.
    That might break some code, though :)

    Cheers,
    mwh

    --
    I have no disaster recovery plan for black holes, I'm afraid. Also
    please be aware that if it one looks imminent I will be out rioting
    and setting fire to McDonalds (always wanted to do that) and
    probably not reading email anyway. -- Dan Barlow
    Michael Hudson, Oct 7, 2004
    #16
  17. Andr? N?ss

    Dave Pawson Guest

    On Wed, 2004-10-06 at 20:28, Alex Martelli wrote:

    >
    > What about a compromise: allow ! and ? only as the LAST (and NOT only)
    > character of an identifier. This leaves you the !cmd as escape to
    > shell, and you only need to ask users to leave a space before the
    > question mark (as in 'x ?') when they're questing for info about x (or
    > you could switch to ?x -- whatever...).


    > Embedded, yes, trailing, no -- that's the compromise I'm proposing... no
    > 'foo!bar' or 'fee?fie' identifier, just _trailing_ bangs and qmarks...


    +1
    Sounds more than reasonable to me.
    And aligns with other usage?

    --
    Regards DaveP.
    XSLT&Docbook FAQ
    http://www.dpawson.co.uk/xsl
    Dave Pawson, Oct 7, 2004
    #17
  18. Andr? N?ss

    Ville Vainio Guest

    >>>>> "Fernando" == Fernando Perez <> writes:

    Fernando> That, and the fact that in _my_ opinion,
    Fernando> names-with-embedded-punctuation look ugly :)

    Hear, hear!

    Abundant ! and ? characters in functions are a good reason to *avoid*
    Scheme. All that shift-pressing and noisy code doesn't suit my
    aesthetic sensibilities.

    It's already legal to use a convention like:

    mystruct.sortD() # destructive

    if symbolP(obj): ... # predicate

    I estimate that ! and ? would be cute for a while, but would soon
    become annoying, especially if *every* predicate suddenly started to
    have "?".

    --
    Ville Vainio http://tinyurl.com/2prnb
    Ville Vainio, Oct 7, 2004
    #18
  19. Andr? N?ss

    Andrew Dalke Guest

    Ville Vainio wrote:
    > Abundant ! and ? characters in functions are a good reason to *avoid*
    > Scheme. All that shift-pressing and noisy code doesn't suit my
    > aesthetic sensibilities.


    > mystruct.sortD() # destructive
    >
    > if symbolP(obj): ... # predicate


    But that means pressing the shift key for the 'D', '(', ')'
    and 'P' characters. You answer doesn't address your first
    complaint.


    Do you prefer Ruby's parens free call syntax? Or is it
    that from practice you no longer consider '(', ')' and ':'
    to be noisy?


    def symbolP x
    return x>0
    end

    if symbolP 5 then
    print "Positive!\n"
    end

    contains less noise than

    def symbolP(x):
    return x > 0

    if symbolP(5):
    print "Positive!"


    Andrew
    Andrew Dalke, Oct 7, 2004
    #19
  20. Andr? N?ss

    Ville Vainio Guest

    >>>>> "Andrew" == Andrew Dalke <> writes:

    >> mystruct.sortD() # destructive
    >> if symbolP(obj): ... # predicate


    Andrew> But that means pressing the shift key for the 'D', '(',
    Andrew> ')' and 'P' characters. You answer doesn't address your
    Andrew> first complaint.

    It doesn't - and I wouldn't use that convention unless imposed by an
    authority. But others would be free to do so even now.

    Incidentally, I've remapped my keyboard so that ( and ) don't require pressing
    shift.

    Andrew> Do you prefer Ruby's parens free call syntax? Or is it
    Andrew> that from practice you no longer consider '(', ')' and ':'
    Andrew> to be noisy?

    I like "()", mostly because the cleaner code would not be worth the
    increased semantic complexity. I don't mind ":", I actually like the
    look of it.

    --
    Ville Vainio http://tinyurl.com/2prnb
    Ville Vainio, Oct 8, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ares Lagae
    Replies:
    8
    Views:
    440
    Ares Lagae
    Sep 24, 2004
  2. Eric Pederson
    Replies:
    1
    Views:
    366
    Paul Foley
    Oct 7, 2004
  3. wanwan
    Replies:
    3
    Views:
    415
    Alex Martelli
    Oct 14, 2005
  4. Cirene
    Replies:
    5
    Views:
    569
    Cirene
    May 17, 2008
  5. News123
    Replies:
    2
    Views:
    455
    John Machin
    Nov 26, 2008
Loading...

Share This Page