PYTHON Magic constants

is this article helpful?
|
Python replacement for PHP's Magic constants [ edit | history ]
This is not exact replacements but here are some ways to achieve the same.

Some of the examples below make use of the 'inspect' module. This module can be very useful for debugging purpose and you can get with it much more that what described here. If you want to use it, don't forget to put the following line in the start of your script:
import inspect


The 'logging' module is also very useful and many times even better, since it's designed to be used for logging and debugging. For example:
import logging

logging.basicConfig(level=logging.INFO,
    format='%(asctime)s %(levelname)-8s %(filename)s:%(lineno)-4d: %(message)s',
    datefmt='%m-%d %H:%M',
    )
logging.debug('A debug message')
logging.info('Some information')
logging.warning('A shot across the bow')

# Should print the following (with current date/time of course)
#10-19 19:57 INFO     test.py:9   : Some information
#10-19 19:57 WARNING  test.py:10  : A shot across the bow

Also see http://www.oreillynet.com/pub/a/python/2005/06/02/logging.html

So here are the proposed replacements:
(The ones listed below is the popular ones, if you need something else, see the included links and write your solution here)
  • __LINE__
    The current line number of the file.
    current_line = inspect.currentframe().f_lineno
    


  • __FILE__
    The path and filename of the current file. If used inside an include, the name of the included file is returned.
    this_file = inspect.currentframe().f_code.co_filename
    


    Note:
    This is different from '.__file__', this is used to find the file that was used to load a module. for example:
    import datetime
    print 'The "datetime" module was loaded from the file: ' + datetime.__file__
    


  • __DIR__
    The directory of the file. If used inside an include, the directory of the included file is returned

  • __FUNCTION__
    The function name
    inspect.getframeinfo(inspect.currentframe()).function
    


  • __CLASS__
    The class name.

  • __METHOD__
    The class method name

  • __NAMESPACE__

PHP Magic constants

PHP original manual for Magic constants [ show | php.net ]

Magic constants

PHP provides a large number of predefined constants to any script which it runs. Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.

There are seven magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it's used on in your script. These special constants are case-insensitive and are as follows:

A few "magical" PHP constants
Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__ The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__ The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__METHOD__ The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).

See also get_class(), get_object_vars(), file_exists() and function_exists().