473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

overriding built-ins

dan
I really have two related questions.

1) Is it possible (without recompiling and changing the core Python
behavior) to write functions that utilize the same syntax as built-in
functions? IE, can I create a function that does this:
printCaps "hello" #note no parentheses

HELLO
2) Is it possible to do this with the built-in keywords themselves, ie
override functions such as print?

thanks in advance -
Jul 18 '05 #1
7 2894

"dan" <da*******@yahoo.com> wrote in message
news:fb**************************@posting.google.c om...
I really have two related questions.

1) Is it possible (without recompiling and changing the core Python
behavior) to write functions that utilize the same syntax as built-in
functions? IE, can I create a function that does this:
printCaps "hello" #note no parentheses
HELLO


There is no special syntax for built-in functions.
If you really meant your example the way you wrote it,
that's a statement, not a function, in which case the answer
is a categorical no.

Built-in functions are simply functions that are in a special
dictionary that Python searches after not finding the requested
function anywhere else. If you want to replace one, feel free.
Then don't feel slighted when nobody else wants to deal with
your program... [grin.]

The Python developers are considering making at least some
built-ins immutable and not overridable, though. That is, not changable at
run time.
The reason is performance - a large number of built-ins are
simply calls to magic methods, so there's a performance gain
to avoiding the search and extra method invocation required
to go through the built-in dictionary for functions like len().
2) Is it possible to do this with the built-in keywords themselves, ie
override functions such as print?
No.

John Roth
thanks in advance -

Jul 18 '05 #2
dan wrote:
I really have two related questions.

1) Is it possible (without recompiling and changing the core Python
behavior) to write functions that utilize the same syntax as built-in
functions?
Yes, but don't confuse functions with statements. It seems you
are systematically doing just that.
IE, can I create a function that does this:
printCaps "hello" #note no parentheses
HELLO


No. You are thinking of "imitating" print, which is a STATEMENT,
*NOT* a built-in function.

Examples of built-in functions are input, hex, max. What they have
in common, which sharply distinguishes them from statements: they
use perfectly normal function syntax, and their names are not in any
way reserved.
2) Is it possible to do this with the built-in keywords themselves, ie
override functions such as print?


print is a statement, not a function. No, there is no way to use
any Python keyword except as Python uses it.
Alex

Jul 18 '05 #3
dan wrote:
1) Is it possible (without recompiling and changing the core Python
behavior) to write functions that utilize the same syntax as built-in
functions? IE, can I create a function that does this:
printCaps "hello" #note no parentheses
HELLO


No. This is not possible.
2) Is it possible to do this with the built-in keywords themselves, ie
override functions such as print?


print is a statement, not a function. Builtin functions can be overwritten.
Statements cannot be overwritten. You can override functions like len, map,
etc. though, but it is not recommended.

Gerrit.

--
53. If any one be too lazy to keep his dam in proper condition, and
does not so keep it; if then the dam break and all the fields be flooded,
then shall he in whose dam the break occurred be sold for money, and the
money shall replace the corn which he has caused to be ruined.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

Jul 18 '05 #4
dan
Thanks for the responses, and my apologies for getting the terminology
confused. What I really should have asked is this:

* Is there any way to create your own statements?
and,
* if so, can they override the names of built-in statements such as
'print'?

To which the answers seem to be, no, and definitely not (with a strong
hint of "why would you ever want to do that"?)

da*******@yahoo.com (dan) wrote in message news:<fb**************************@posting.google. com>...
I really have two related questions.

1) Is it possible (without recompiling and changing the core Python
behavior) to write functions that utilize the same syntax as built-in
functions? IE, can I create a function that does this:
printCaps "hello" #note no parentheses

HELLO
2) Is it possible to do this with the built-in keywords themselves, ie
override functions such as print?

thanks in advance -

Jul 18 '05 #5
dan wrote:
Thanks for the responses, and my apologies for getting the terminology
confused. What I really should have asked is this:

* Is there any way to create your own statements?
and,
* if so, can they override the names of built-in statements such as
'print'?

To which the answers seem to be, no, and definitely not (with a strong
Right.
hint of "why would you ever want to do that"?)


Actually, I suspect it's pretty obvious to most respondents why one
might want to create their own language, embedded inside Python but
enriched and modified. Guido himself is on record as having the far
_dream_ of one day managing to let people define "application-specific
languages" inside Python -- IF he can come up with a smart way to
do it, one that won't destroy Python's simplicity and utter
straightforwardness. I suspect that "strong hint" reflects a deep
distrust that any such silver bullet exists, and a deep fear that in
the pursuit of such elusive goals Python's simplicity and philosophy
of "there should be only one way to do it" might be destroyed.

There exist languages which have very powerful macro systems. Common
Lisp is the canonical example, but if you prefer syntax that is rather
more Python-like you might look into Dylan instead. Lisp and Dylan
also share the powerful concept of "multiple dispatch", a way to do
OO that is as superior to C++'s or Python's as the latter is superior
to that of _purely_ single-inheritance languages [ones without even
such helpers as Java interfaces or Ruby mixins]. Some languages do
not really have such power, but can still come close -- e.g., in
Smalltalk, "IF" is not a _statement_, just a method to which you pass
code blocks, so there is little impediment to piling up "more of the
same" to make your own application-specific language inside it; to
a lesser extent, you can do somewhat similar things in Ruby -- and
Perl has its own inimitable way (google for "Lingua Latina Perligata"
if you're REALLY TRULY into such... ahem... divertissements:-).

A widespread feeling around the Python community is that Python's own
uniqueness is SIMPLICITY. Python is straightforward and thereby most
productive because it DOESN'T EVEN TRY to let you bend its syntax in
strange ways (in that, it's closest to Java -- i.e., it even lacks the
puny "preprocessor" that C and C++ come with). If you want an
application-specific language, you design and implement it in the good
old time-trusted way -- lexx and yacc, or their Python equivalents (of
which there are quite a few) -- Python stays Python, your language
is your own, and everybody (hopefully) is happy...;-).
Alex

Jul 18 '05 #6
dan
Alex Martelli <al***@aleax.it> wrote in message news:<Yf**********************@news2.tin.it>...
....
Actually, I suspect it's pretty obvious to most respondents why one
might want to create their own language, embedded inside Python but
enriched and modified. Guido himself is on record as having the far
_dream_ of one day managing to let people define "application-specific
languages" inside Python -- IF he can come up with a smart way to
do it, one that won't destroy Python's simplicity and utter
straightforwardness. I suspect that "strong hint" reflects a deep
distrust that any such silver bullet exists, and a deep fear that in
the pursuit of such elusive goals Python's simplicity and philosophy
of "there should be only one way to do it" might be destroyed.
Excellent response -- of course that is exactly what I would like to
do. And I appreciate that Python's simplicity and uniformity can be
seen as pluses. Perhaps this is a request for a different type of
meta-language, and shouldn't be shoe-horned into Python. I'll look at
Dylan and some of the other stuff you mention.

Of course Python would be a great language in which to develop a
parser/interpreter/compiler for such a language. Perhaps it would
make sense to develop a new syntax that shares the Python bytecodes
and import methodology, so you could start with a large library of
support modules and such.

However that brings up an interesting issue: the plethora of new
languages with bytecode interpreters (Java, Python, C#) are starting
to concern me. It seems like there could be a place for some sort of
standardized, low-level bytecode syntax, so that work on things like
JIC compilation can occur independently of high-level changes and
alternatives to source syntax.

There exist languages which have very powerful macro systems. Common
Lisp is the canonical example, but if you prefer syntax that is rather
more Python-like you might look into Dylan instead. Lisp and Dylan
also share the powerful concept of "multiple dispatch", a way to do
OO that is as superior to C++'s or Python's as the latter is superior
to that of _purely_ single-inheritance languages [ones without even
such helpers as Java interfaces or Ruby mixins]. Some languages do
not really have such power, but can still come close -- e.g., in
Smalltalk, "IF" is not a _statement_, just a method to which you pass
code blocks, so there is little impediment to piling up "more of the
same" to make your own application-specific language inside it; to
a lesser extent, you can do somewhat similar things in Ruby -- and
Perl has its own inimitable way (google for "Lingua Latina Perligata"
if you're REALLY TRULY into such... ahem... divertissements:-).

A widespread feeling around the Python community is that Python's own
uniqueness is SIMPLICITY. Python is straightforward and thereby most
productive because it DOESN'T EVEN TRY to let you bend its syntax in
strange ways (in that, it's closest to Java -- i.e., it even lacks the
puny "preprocessor" that C and C++ come with). If you want an
application-specific language, you design and implement it in the good
old time-trusted way -- lexx and yacc, or their Python equivalents (of
which there are quite a few) -- Python stays Python, your language
is your own, and everybody (hopefully) is happy...;-).
Alex

Jul 18 '05 #7
dan:
However that brings up an interesting issue: the plethora of new
languages with bytecode interpreters (Java, Python, C#) are starting
to concern me. It seems like there could be a place for some sort of
standardized, low-level bytecode syntax, so that work on things like
JIC compilation can occur independently of high-level changes and
alternatives to source syntax.


The .Net system was supposed to help some of that. As I recall, some
changes were made to make it easier to support tail recursive languages.

For something targeted to dynamically typed languages, look up
"Parrot", which is part of the Perl6 effort and which is also supposed
to support Python. There's a challenge out that Parrot will run some
appropriate benchmark (yet to be decided) faster than the CPython
implementation by some time next year. (OSCON?)

Andrew
da***@dalkescientific.com
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
I want that every time the built-in 'mysql_query' function is called another user function is also called. But i can't call the user function explicitly, I can only call the built-in function. ...
1
by: Andrew Durdin | last post by:
On Fri, 20 Aug 2004 23:28:20 -0700, Robert Brewer <fumanchu@amor.org> wrote: > > The first barrier is that 'or' and 'and' get compiled down to jump > codes, as opposed to operations: >...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
2
by: callmebill | last post by:
I'm having a tough time figuring this one out: class MyKBInterrupt( ..... ): print "Are you sure you want to do that?" if __name__ == "__main__": while 1: print "Still here..."
0
by: Ryan Mack | last post by:
I'm doing development on an embedded system using a GCC 2.96 MIPS cross compiler and a minimal C standard library replacement. The system loads at startup a base executable. The base executable...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
4
by: sultanwadood | last post by:
Hi all. I need to know how to restore or roll back to built-in function after overriding like following example. String.prototype.substr= function() { return "";} Now I want to use the one...
10
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This...
0
by: =?Utf-8?B?aGZkZXY=?= | last post by:
Hello, I have an ASP.NET website that I built in .NET 2.0. I created a partial class that inherits from the System.Web.UI.Page that I will call SpecialPage for purposes of this discussion. ...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.