473,804 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2922

"dan" <da*******@yaho o.com> wrote in message
news:fb******** *************** ***@posting.goo gle.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


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.go ogle.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 "applicatio n-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
straightforward ness. 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 "preprocess or" 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 "applicatio n-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
straightforward ness. 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 "preprocess or" 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***@dalkescie ntific.com
Jul 18 '05 #8

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

Similar topics

5
8049
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. Any idea? Much thx in advance.
1
1535
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: > ....so you'd be hacking a completely different section of the compiler to > enable such overriding.
3
4203
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 the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
2
3529
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
1131
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 then loads one DLL after another during execution to reduce memory usage. The base executable statically links in a library which overrides the default malloc (and related functions) and the built-in operator new/delete. That works fine and as...
3
1560
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
2308
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 already built-in. I shall be very thankful if some one could help me. Sultan Wadood
10
6958
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 is my class: class RemoteXmlDataSource : System.Web.UI.WebControls.XmlDataSource And I want to override
0
1085
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. One reason that I did this is to add global error logging to my app. I overrode the OnError method and log errors using a logging framework. there are other reasons too, but I'll spare you ;-)
10
105210
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.