MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Overriding Mathematica function Print

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64339] Re: Overriding Mathematica function Print
  • From: albert <awnl at arcor.de>
  • Date: Sun, 12 Feb 2006 04:00:24 -0500 (EST)
  • References: <dscca1$p7a$1@smc.vnet.net> <dset8h$j9s$1@smc.vnet.net> <dsk8ek$i77$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

> is it possible to call the original (system) definition of Print from
> the overridden version of Print (kind alike "inherited" or "super" in
> object-oriented languages)?

this does what you want, although it might look like a workaround if you
look at it from an oo-point of view...

Unprotect[Print, $usemyprint]; 

$usemyprint = True; 

Print[args___] := Block[
     {$usemyprint = False}, 
     Print["------------------"]; 
     Print[args]; 
     Print["------------------"]; 
     ] /; TrueQ[$usemyprint];
      
Protect[Print, $usemyprint]; 

Now you will use your overwritten definition by default, but can switch to
the regular version whenever you want:

Print[a, b, c]

Block[{$usemyprint = False}, Print["hallo"]]


There is another possibility which involves context manipulation and shadows
System`Print on purpose. It is probably only a good idea if you have a
decent understanding what context/namespaces do and how they work, which
might well be the case if you have an oo-background:

My`Print[args___] := (
    System`Print["------------------"]; 
    System`Print[args]; 
    System`Print["------------------"]; 
); 
PrependTo[$ContextPath, "My`"];

Now Print or My`Print is your overwritten version, while System`Print is
what it says, the original System Print...

Personally I would do things like this only with a good reason because it
might break or alter other code (including all packages you might use). The
only good reason I can think of is that you want alter the Print-Behaviour
in code that already exists and you don't want or can touch. For most other
cases it is more straightforward to just use another symbol (name) for your
personalized version, e.g. myPrint and use that throughout your code. Then
there is no conflict with the system Print at all and you need no
"workarounds", keep your fingers of other code and just can call Print
within your definition for myPrint. Switching back to System`Print for all
your code is also easy by evaluating myPrint=Print once.

hth,

Albert


  • Prev by Date: Re: Comparison of Mathematica on Various Computers
  • Next by Date: projection of contourplot
  • Previous by thread: Re: Overriding Mathematica function Print
  • Next by thread: Using sockets for "downloader" application in Mathematica