473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Proposal for removing self - followup

>> Well make it three cents and do an implementation, if you really
care.
Otherwise, it's just words...


I can't post a reply to that thread in Google. So I am posting the
third cent here.

Here is my implementation of the PythonCOM style __private__ ()
attributes for Python.
http://aspn.activestate.com/ASPN/doc...ServerCom.html

After

http://groups.google.com/groups?q=g:...bellglobal.com

the only thing left was __private__

I have done very little testing and this is my first use of
metaclasses, so watch out.

It uses the bytecode hacking module from
http://bytecodehacks.sourceforge.net/

from bytecodehacks.code_editor import Function

class PrivateType(type):
def __new__(cls, className, bases, classDict):
if '__private__' in classDict and
len(classDict['__private__']) > 0 :
# create a map of to-be-renamed functions
private_map = {}
for private_attribute in classDict['__private__']:
private_map[private_attribute] = '_%s__%s' %
(className, private_attribute)

def MutatedFunc(func):
bytecode = Function(func)
for iter, co_name in
enumerate(bytecode.func_code.co_names):
if co_name in private_map.keys():
bytecode.func_code.co_names[iter] =
private_map[co_name]
return bytecode.make_function()

# refactor references within functions
func_type = type(lambda x: x)
for attrib_name, attrib in classDict.items():
if type(attrib) == func_type:
classDict[attrib_name] = MutatedFunc(attrib)

# mangle private attributes
private = classDict['__private__']
for private_attribute in private:
temp = classDict[private_attribute]
del classDict[private_attribute]
privatized_attribute = '_%s__%s' % (className,
private_attribute)
classDict[privatized_attribute] = temp
return type.__new__(cls, className, bases, classDict)
else:
return type.__new__(cls, className, bases, classDict)

class PrivateClass(object):
__metaclass__ = PrivateType

if __name__ == '__main__':
class Foo(PrivateClass):
__private__ = ['private_func']

def public_func(self):

print "calling public func: ",
print "public"
print "calling private func: ",
self.private_func()

def private_func(self):
print "private"

foobar = Foo()
foobar.public_func()
#foobar.private_func()
Jul 18 '05 #1
0 1058

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

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
15
by: Brent W. Hughes | last post by:
When doing object-oriented stuff, it bothers me to have to type "self" so many times. I propose that Python allow the programmer to optionally type ".variable" instead of "self.variable" to mean...
31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
18
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object):...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
3
m6s
by: m6s | last post by:
Hello to all, I am having trouble removing an item from list, which the item is a list by itself. <code> for self.line in self.filtered: if self.appid: if self.line <> self.appid: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.