injecting methods into a class

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

injecting methods into a class

Postby mouseroot » Mon Feb 18, 2013 1:13 am

Lets say you have a file that has some methods/code inside it

example of funcs.py
Code: Select all
#some methods
def printHello():
    print "Hello world"

def printGoodbye():
    print "goodbye"


main.py:
Code: Select all
class App:
    def __init__(self,file):
        exec open(file,"r") in self.__dict__


This lets you "inject" the methods into your class
so now you can do something similar to this
Code: Select all
app = App("func.py")
app.printHello()
app.printGoodbye()


this is also known as evaluting the code in func.py inside the __dict__
dictionary inside the class thus adding the methods to that class

I personally use this in a web server for portable python code
similar to how some apps push single javascript functions to the cloud

it could also be used to "hot-load" functions into the class
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: injecting methods into a class

Postby stranac » Mon Feb 18, 2013 11:18 am

This...seems like a really bad idea.
And I don't see a practical use for it.

There's no need to exec the file.
If you want to have methods from a different file in your class, just put them into a class of their own and inherit from it.
For your example, simply importing the module should also work.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: injecting methods into a class

Postby mouseroot » Tue Feb 19, 2013 12:52 am

well as I have it..this is a bad example however you can limit
what all can be called within the loaded files

for example lets say I just math functions evaluated
you can set the globals dict to functions that you want
evaluated

I do admit this does feel/look like a bad practice but
like I said this is more for a cloud solution.
where these functions do not exist at run time

ofcourse you could just use the following

Code: Select all
try:
    from func import *
except ImportError,er:
    print err
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: injecting methods into a class

Postby micseydel » Tue Feb 19, 2013 1:11 am

"from whatever import *" is highly discouraged because of namespace pollution and potential clashes. Also, the comma in the except syntax is deprecated, us "as" instead.

I also think that this is a messy way to do things. I believe it's called monkey-patching in Ruby, but it's intentionally left out of Python because it leads to poorly designed (if you can even call it designed) structure.

Just do it right the first time, and if that's not possible, update it correctly, don't try to patch something that is bad.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: injecting methods into a class

Postby mouseroot » Tue Feb 19, 2013 1:49 am

this is one of the reasons I joined this community

I admit i have never had the "production" environment in mind
and I always write code that have made other coders barf in their mouths
and I am self taught on pretty much all my coding because when I was in
school they wanted to teach java and at the time I wanted C++ and assembler
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: injecting methods into a class

Postby micseydel » Tue Feb 19, 2013 2:37 am

I've never been formally taught in Python, but I eventually picked up mostly good habits. (Admittedly, I was looking at some code last night that I wrote three years ago, and gosh, it's bad.) Spend more time in the community, and read code you find online; when you read others' code you will realize how bad some things are!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests