Python - cool things I've discovered

Started by Ryex, October 12, 2010, 09:45:35 pm

Previous topic - Next topic

Ryex

October 12, 2010, 09:45:35 pm Last Edit: October 13, 2010, 02:44:46 am by Ryexander
Ruby's alais in Python


Well I was thinking about the use of the alias method in Ruby and wanted to know how to do something similar in Python. the problem is that Python don't allow for already defined classes to be reopened and added to. if you did this

class Person:
   def __init__(self, name):
       self.name = name
   def print_name(self):
       print self.name

class Person:
   def good_name(self):
       print "%s is a good name" % self.name

bob = Person("bob")


you would get an error that "this constructor takes no arguments" this is because the second class statement "class Person:" completely overwrites the old class with a new one. This annoyed me because in Ruby the second class statement would open the previous class statement and add to it.

but then I realized every name attached to a Python object is an attribute(property) and is publicly modifiable. so you could do this:

class Person:
   def __init__(self, name):
       self.name = name
   def print_name(self):
       print self.name


def good_name(self):
   print "%s is a good name" % self.name

Person.good_name = good_name

bob = Person("bob")
bod.good_name() #> "bob is a good name"


following this line of thinking I realized that this is possible too


class Person:
   def __init__(self, name):
       self.name = name
   def print_name(self):
       print self.name


def good_name(self):
   print "%s is a good name" % self.name

Person.good_name = good_name

Person.print_name_later = Person.print_name

def print_name(self):
   self.print_name_later()
   self.good_name()

Person.print_name = print_name

del good_name
del print_name

bob = Person("bob")
bod.print_name() #> "bob" /n "bob is a good name"


and there you have it I added to a pre-existing python method by first giving the Person class a new property and setting its value to the function object of the method I wanted to overwrite
then defining a new method where I call the old method renamed and call another method
and then setting the Person class property that holds a reference to the old function object to the new function object

the principle and effect tis the exact same as this use of Ruby's alias

EDIT: i just realized that it would be a good idea to add these lines
del good_name
del print_name

this ensures that the global names get removed just ensures that no conflicts arise
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Nice idea using Python's object oriented implementation. I wouldn't have thought of it.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

thanks, it takes a bit more code than ruby but its a great trick to know
I'll be posting other tricks find out here too
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />