Author Topic: Python - cool things I've discovered  (Read 2578 times)

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5135
  • LV: 198
  • Gender: Female
  • Wants to write a compiler for fun
    • View Profile
Python - cool things I've discovered
« on: October 13, 2010, 03:45:35 AM »
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
Code: [Select]
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:
Code: [Select]
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

Code: [Select]
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
Code: [Select]
del good_name
del print_name
this ensures that the global names get removed just ensures that no conflicts arise
« Last Edit: October 13, 2010, 08:44:46 AM by Ryexander »
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.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 20007
  • LV: 651
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Python - cool things I've discovered
« Reply #1 on: October 13, 2010, 07:28:24 AM »
Nice idea using Python's object oriented implementation. I wouldn't have thought of it.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5135
  • LV: 198
  • Gender: Female
  • Wants to write a compiler for fun
    • View Profile
Re: Python - cool things I've discovered
« Reply #2 on: October 13, 2010, 08:46:20 AM »
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.

DropBox, the best free file syncing service there is.
(click to show/hide)