On Thu, Jan 21, 2016 at 5:49 AM, Steven D'Aprano <st...@pearwood.info> wrote: > On Wed, Jan 20, 2016 at 09:42:29PM -0600, boB Stepp wrote: > >> So I really only have one question: Why not make Python's >> *traditional* name, "self", mandatory? Why give the programmer this >> kind of choice? [OK, that was two questions.] > > Why bother making it mandatory? That just makes more work for the > compiler -- it has to decide that a function is inside a class, and > therefore apply a restriction to the first argument. Most of the time, > giving the programmer more freedom is less work. > > And what happens if you do this? > > class X: > pass > > def func(this): > print("instance %r called method" % this) > > X.method = func
Am I understanding this correctly? It appears to me that you have successfully added a method to class X from outside of class X! If this is the case, then this clarifies some things I was wondering about in my other thread. > And what are we supposed to do with classmethods and staticmethods? They > shouldn't take a "self" argument at all, but they start off life as a > regular function, just like ordinary instance methods. > > And of course, there are Metaclasses. You might need a metaclass method > that needs to deal with all three levels of the hierarchy: the > metaclass, the class it creates, and the instance of that class. > > class Meta(type): > def metamethod(meta, cls, self): > ... I scanned the article that Danny gave a link to. I am not ready to go in depth into these concepts at this time, but I cannot help but wonder about the possibilities these concepts open up? At work I find myself writing Python to generate correct scripting language commands for the proprietary software I use at work. What if I could write a Python program which thoroughly captured the entire proprietary scripting language into a much easier to use interface? Their scripting language has an underlying OO structure that is not exposed to my prying eyes. But *if* I could create a class structure that was sufficiently similar to theirs that it seamlessly generated the correct messages that their OOP style uses, then things might be much easier for my projects. Hmm... I guess I am trying to wrap my mind around this incredible power and flexibility that Python provides. I thought I had asked a relatively harmless question. But it generated some strong responses! It seemed like "self" had a similar utility of use as "print" to me. After all, we can't redefine "print", can we? But now I realize that I can do exactly that if I so choose. That is both fascinating and scary! _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor