Tuesday, December 21, 2010

Access Private Variables and Methods in Python

If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores. This invokes Python's name mangling algorithm, where the name of the class is mangled into the attribute name. This helps avoid attribute name collisions should subclasses inadvertently contain attributes with the same name.
Private variables and methods are just name mangling in Python. You can access a private variable by using _ClassName__privateVarName.
class Foo:
def __init__(self):
self.__privateVar = 12

def __privateMethod(self):
return 'Private method invoked'

def greetings(self):
return 'Hello World!'

>>> foo = Foo()
>>> foo.greetings() #public method
'Hello World' #output
>>> foo._Foo__privateVar #private variable
12 #output
>>> foo._Foo__privateMethod() #private method
'Private method invoked' #output

Thursday, December 16, 2010

Flex 4 Syntax Highlighting in Vim

There is syntax highlighting for Flex 3 and ActionScript 3 *. For Flex 4 syntax highlighting you have to make only a few changes to this mxml.vim file as follows:
1. Find the line:
syn keyword mxmlSpecialTagName  contained mx:Script mx:Style
and replace it with:
syn keyword mxmlSpecialTagName  contained mx:Script mx:Style fx:Script fx:Style
2. If you have found the above line go five line down and you will a sync with mx:Script tag. So you add these one too:
syn region Script start=+]*>+ keepend end=++me=s-1 contains=@mxmlScript,mxmlCssStyleComment,mxmlScriptTag,@htmlPreproc
syn region mxmlScriptTag contained start=++ contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent
Alternatively you can download the vim script file from vim.org