I mean:
class A:
__metaclass__ = Selfless
def __init__(a , b):
self.a = a
self.b = b
Is made to work from this example.
One common critic to Python is the need of the explicit "self" parameter declaration on function Methods. No matter how this design is optimal in terms of internal consistency, keeping things explicit, and reducing the keyword count, people keep complaining about it.
Given the access granted to the inner workings of the Language, it is easy to arrange an alternate mechanism for class methods that could preclude the declaration of a first parameter to a method. Therefore, people who are seeing complaining could try some shots of "selfless" Python.
My approach here is just as follows: I create a metaclass that uses an alternate way to fetch function methods inside the classes. With the normal "type" metaclass - i.e. normal "new style" classes - any functions declared in a class body are substituted, at class creation time, for a descriptor. When the attribute with the function name in a class, or instance of it, is retrieved, this descriptor creates a method object that wraps the original function. (And yes, the method object is created at attribute access time). This method object, when called, prepends the "instance" object as the first parameter in the list, and forwards the call to the wrapped function.
In this "selfless" proof of concept, I change the functions in the class body for descriptor objects, but descriptors that behave differently: instead of wrapping the function in a method object, they recreate the function when the method is acessed.
A new function object is created with the same code object, the same name, the same closure, but, with a different global dictionary. The global dictionary of the original function is copied and changed to include an entry named "self" which points to the object instance. (Or "None" if the function is retrieved from the class, not from an instance).
Therefore, inside these selfless methods, the name "self" can be used without ever being declared: it will exist as a global vairable at execution time, and point to the instance. Just like the parameter self exists as a local variable and points to the instance in regular Python methods.
This should not be used in production code - it is a good proof of concept - but of course there should be lots of strange and inconsistent behavior associeated with it. One of which is the fact methods can not be used "unbounded" as one does with ordinary python methods, explicitly passing an instance object as the first parameter.
Asa final consideration, Pedro Werneck had an implementation of this "feature" before here: http://www.python.org.br/wiki/NoSelf - but unlike Werneck's example, I don't fiddle with the bytecode or anyother thing to "rebuild" the bytecode so that references to an unexisting local self variable are rewritten, and "self" is artificially inserted as a local variable at runtime. The work presented here is an order of magnitude simpler, and possibly less subject to colateral effetcs.
Nonetheless: I warn again - this is just a toy! Don't use this in "real life"!
Other than that, it works finner and in a simpler way than I expected.
# -*- coding: utf-8 -*-
from types import FunctionType
"""
Changes classes so that each method has its instances changed so
that "self" is a global variable pointing to the instance.
"""
class SelflessDescriptor(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, class_):
new_globals = self.func.func_globals.copy()
new_globals["self"] = instance
new_func = FunctionType(self.func.func_code,
new_globals, self.func.func_name,
self.func.func_defaults,
self.func.func_closure)
return new_func
class Selfless(type):
def __new__(cls, name, bases, dict_):
for key, val in dict_.items():
if isinstance(val, FunctionType):
dict_[key] = SelflessDescriptor(val)
return type(name, bases, dict_)
if __name__ == "__main__":
__metaclass__ = Selfless
class A:
def __init__(a, b):
self.a = a
self.b = b
a = A("Hello", "World!")
print a.a, a.b
Now you just need to extend it to act like Java does put all variables in the class into the scope, not just self - they'll really like that. ;-)
ReplyDeleteclass A:
a = None
b = None
def __init__(a_, b_):
a = a_
b = b_
def get_a():
return a
(Yuck. Having to use non-breaking spaces for spacing is definitely not a nice way to have it.)
In follow-up on that, I've got it to work, provided you don't do any assignment. Due to the required use of globals rather than locals, it can't work with locals.
ReplyDeleteHere's the code to laugh at (won't work directly with copy and paste as I had to use nbsp - Ctrl-V 160 - to get spacing to display right).
# -*- coding: utf-8 -*-
from types import FunctionType
"""
Changes classes so that each method has its instances changed so
that "self" is a global variable pointing to the instance.
"""
class SelflessDescriptor(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, class_):
if hasattr(self, 'getting'): return self.func
self.getting = True
new_globals = self.func.func_globals.copy()
new_globals["self"] = instance
new_globals.update((i, object.__getattribute__(instance, i)) for i in dir(instance))
new_func = FunctionType(self.func.func_code,
new_globals, self.func.func_name,
self.func.func_defaults,
self.func.func_closure)
del self.getting
return new_func
class Selfless(type):
def __new__(cls, name, bases, dict_):
for key, val in dict_.items():
if isinstance(val, FunctionType):
dict_[key] = SelflessDescriptor(val)
return type(name, bases, dict_)
if __name__ == "__main__":
__metaclass__ = Selfless
class A:
def __init__(a_):
# Unfortunately, a = [a_] would not work.
self.a = [a_]
def add_word(s):
# a += [s] would not work
a.extend([s])
a = A('Hello')
a.add_word('World!')
print ' '.join(a.a)
Patches welcome ;-)
Hi Chris - yes, a ++ fan, friend of mine told me that he'd like this "complete selfless" approach. After all, having "self." is so much herder to read than having those -> and "*" and "Virtual Void" all over the place. :-)
ReplyDeleteI've been thinking on it - it could be done by declaring the variables as global inside the methods, and turning the global dictionary that is created to the method into a specialized object that would commit its changes back to the instance.
ut I am bit afraid of doing that now that friend said he'd actually _use_ such a thing. :-)
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleterpa Training in Chennai
rpa Training in bangalore
rpa Training in pune
blueprism Training in Chennai
blueprism Training in bangalore
blueprism Training in pune
rpa online training
It is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
selenium training in chennai
A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
ReplyDeletejava training in chennai | java training in bangalore
java online training | java training in pune
java training in chennai | java training in bangalore
Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this.
ReplyDeletepython training in pune
python online training
python training in OMR
I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
ReplyDeleteiosh course in chennai
Thanks admin for spending your valuable time to write an excellent article.
ReplyDeleteSelenium training in Chennai
Selenium Courses in Chennai
best ios training in chennai
.Net coaching centre in chennai
French Classes in Chennai
Big Data Training in Chennai
Best PHP training in chennai
PHP Training
Good post thanks for author
ReplyDeleteblue prism training course in chennai
Awesome!!! Can't tell how pleased i am. :)
ReplyDeletehttps://belize24.de
Belize Guatemala Reise
Urlaub in Belize
Nice post. I learned some new information. Thanks for sharing.
ReplyDeleteArticle submission sites
Technology
Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your postand look forward to more.
ReplyDeleteHi, i have read this article , it was very useful, everything is explained in detail about. if you want to learn more about this particular topic then follow this given link : https://www.excelr.com/data-science-course-training-in-bangalore/
ReplyDeleteHi, i have read this article , it was very useful, everything is explained in detail about. if you want to learn more about this particular topic then follow this given link :
ReplyDeletehttps://www.excelr.com/data-science-course-training-in-bangalore/
Hi, i have read this article , it was very useful, everything is explained in detail about. if you want to learn more about this particular topic then follow this given link : https://www.excelr.com/data-analytics-certification-training-course-in-bangalore/
ReplyDeleteI feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.Technologies
ReplyDeleteHi, i have read this article , it was very useful, everything is explained in detail about.
ReplyDeleteif you want to learn more about this particular topic then follow this given link : data science courses in bangalore
nice article
ReplyDeleteThis is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck. best data science training in bangalore
ReplyDeletethanks for sharing this information
ReplyDeleteBlue Prism Training in Bangalore
Blue Prism Training in BTM
RPA Training in BTM
RPA Training in Bangalore
Android Training in Bangalore
Android Training in Bangalore
Google Cloud Training in Bangalore
Azure DevOps training in Bangalore
This is also a very good post which I really enjoyed reading
ReplyDelete. It is not every day that I have the possibility to see something like this..
I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
ReplyDeleteData analytics courses
Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.
ReplyDeleteBest PHP Training Institute in Chennai|PHP Course in chennai
Best .Net Training Institute in Chennai
MCSE Training in Chennai
AI Training in Chennai
SEO Training in Chennai
In sql there developer uses to interact with the database. learning sql can be done through sql certification
ReplyDeletesuper post...!
ReplyDeleteinternship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai
ReplyDeleteAwesome,Thank you so much for sharing such an awesome blog.
sap hr courses in bangalore
sap hr classes in bangalore
sap hr training institute in bangalore
sap hr course syllabus
best sap hr training
sap hr training centers
sap hr training in bangalore
Nice blog! Such a good information about data analytics and its future..
ReplyDeleteGood post! I must say thanks for the information.
data analytics course in mumbai
Data analytics Interview Questions
Nice blog! Such a good information about data analytics and its future..
ReplyDeleteGood post! I must say thanks for the information.
data analytics course in mumbai
Data analytics Interview Questions
Study Artificial Intelligence Course in Bangalore with ExcelR where you get a great experience and better knowledge.
ReplyDeleteArtificial Intelligence Course
Study Data Scientist Course in Bangalore with ExcelR where you get a great experience and better knowledge.
ReplyDeleteData Scientist Course
A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
ReplyDeletedigital marketing course
This comment has been removed by the author.
ReplyDeleteThank you for this informative blog
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
Looking at your Comparision, I see the point no 35, you have mentioned BO has the Write-Back capability, where it's not at all there.thanks a lot.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
You have composed an exceptionally educational article with incredible quality substance and all around spread out focuses. I concur with you on a large number of your perspectives and you have me thinking.
ReplyDeleteSEO services in kolkata
Best SEO services in kolkata
SEO company in kolkata
Best SEO company in kolkata
Top SEO company in kolkata
Top SEO services in kolkata
SEO services in India
SEO copmany in India
I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
ReplyDeleteData Scientist Courses Super site! I am Loving it!! Will return once more, Im taking your food likewise, Thanks.
Indeed a great article! I am definitely going to bookmark it to go through it over again after work.
ReplyDeleteSAP training in Kolkata
SAP course in kolkata
SAP training institute in Kolkata
It seems to cover the major information about the topic in points.
ReplyDeleteSAP training in Mumbai
SAP course in Mumbai
SAP training institute Mumbai
Not many writers can persuade me to their way of thinking. You've done a great job of doing that on many of your views here.
ReplyDeleteDenial management software
Denials management software
Hospital denial management software
Self Pay Medicaid Insurance Discovery
Uninsured Medicaid Insurance Discovery
Medical billing Denial Management Software
Self Pay to Medicaid
Charity Care Software
Patient Payment Estimator
Underpayment Analyzer
Claim Status
So luck to come across your excellent blog. Your blog brings me a great deal of fun.. Good luck with the site. ExcelR Data Science Course In Pune
ReplyDeleteI like that article awesome.
ReplyDeletePython Training in Chennai
Python Training in Bangalore
Python Training in Hyderabad
Python Training in Coimbatore
Python Training
python online training
python flask training
python flask online training
Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well. Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course
ReplyDeleteThanks for sharing such a great blog
ReplyDeleteVermicompost manufacturers in Tamilnadu | Vermicompost in Tamilnadu
Vermicompost Manufacturers | Vermicompost Suppliers
Vermicompost in Coimbatore | Vermicompost manufacturers in Chennai
Vermicompost in chennai | Best Vermicompost in chennai
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training inTambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital MarketingTraining in Annanagar
very well explained .I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Velachery
Software Testing Training in Tambaram
Software Testing Training in Porur
Software Testing Training in Omr
Software Testing Training in Annanagar
ReplyDeleteNice article and thanks for sharing with us. Its very informative
Machine Learning Training in Hyderabad
ReplyDeleteNice article and thanks for sharing with us. Its very informative
DATA SCIENCE TRAINING IN HYDERABAD
I think this is a really good article. You make this information interesting and engaging. ExcelR Data Analytics Courses You give readers a lot to think about and I appreciate that kind of writing.
ReplyDeleteThank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
ReplyDeletePython Training in chennai
Python Course in chennai
A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
ReplyDeleteDevOps Training in Chennai
DevOps Course in Chennai
AI Patasala highly acclaimed Python Course in the Hyderabad course is your ideal choice for a career in Python.
ReplyDeletePython Course with Placements in Hyderabad
Nice blog
ReplyDeletecomment link Digital Marketing Courses in Surat
What an insightful take on selfless Python! Your explanation of the concept is clear and thought-provoking. Thanks for sharing your expertise—it’s a pleasure to learn from your blog."
ReplyDeleteData science courses in the Netherlands
Brilliant blog.
ReplyDeleteData science Courses in Berlin