PYTHON define

is this article helpful?
Python replacement for PHP's define [ edit | history ]
Constant types in Python.
__doc__ = """
This is a variation on "Constants in Python" by Alex Martelli, from which the
solution idea was borrowed, and enhanced according suggestions of Zoran Isailovski.

In Python, any variable can be re-bound at will -- and modules don't let you
define special methods such as an instance's __setattr__ to stop attribute
re-binding. Easy solution (in Python 2.1 and up): use an instance as "module"...

In Python 2.1 and up, no check is made any more to force entries in sys.modules
to be actually module objects. You can install an instance object there and take
advantage of its attribute-access special methods (e.g., as in this snippet, to
prevent type rebindings. 

  import consttype
  consttype.magic = 23    # Bind an attribute to a type ONCE
  consttype.magic = 88    # Re-bind it to a same type again
  consttype.magic = "one" # But NOT re-bind it to another type: this raises consttype._ConstError
  del consttype.magic     # Remove an named attribute
  consttype.__del__()     # Remove all attributes

class _consttype:
    class _ConstTypeError(TypeError):
    def __repr__(self):
        return "Constant type definitions."
    def __setattr__(self, name, value):
        v = self.__dict__.get(name, value)
        if type(v) is not type(value):
            raise self._ConstTypeError, "Can't rebind %s to %s" % (type(v), type(value))
        self.__dict__[name] = value
    def __del__(self):

import sys
sys.modules[__name__] = _consttype()

PHP define

PHP original manual for define [ show | ]


(PHP 4, PHP 5)

defineDefines a named constant


bool define ( string $name , mixed $value [, bool $case_insensitive= false ] )

Defines a named constant at runtime.



The name of the constant.


The value of the constant; only scalar and null values are allowed. Scalar values are integer, float, string or boolean values.


If set to TRUE, the constant will be defined case-insensitive. The default behaviour is case-sensitive; i.e. CONSTANT and Constant represent different values.

Return Values

Returns TRUE on success or FALSE on failure.


Example #1 Defining Constants

("CONSTANT""Hello world.");
CONSTANT// outputs "Hello world."
echo Constant// outputs "Constant" and issues a notice.

define("GREETING""Hello you."true);
GREETING// outputs "Hello you."
echo Greeting// outputs "Hello you."


See Also