Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the python built-in methods available in an alternative namespace anywhere?

Are the python built-in methods available to reference in a package somewhere?

Let me explain. In my early(ier) days of python I made a django model similar to this:

class MyModel(models.Model):
    first_name = models.CharField(max_length=100, null=True, blank=True)
    last_name = models.CharField(max_length=100, null=True, blank=True)
    property = models.ForeignKey("Property")

I have since needed to add a property to it. This leaves me with this model:

class MyModel(models.Model):
    first_name = models.CharField(max_length=100, null=True, blank=True)
    last_name = models.CharField(max_length=100, null=True, blank=True)
    property = models.ForeignKey("Property")

    @property
    def name(self):
        return "{} {}".format(first_name, last_name)

So now at runtime I get the error: TypeError: 'ForeignKey' object is not callable. This is happening because the ForeignKey for property has replaced the built-in identifier property. What I would like to be able to do is, instead of @property use @sys.property (or something similar).

Note: I already know about the workaround of moving the name property above the declaration of the property field. I am not so concerned about this particular case as I am the main question of alternative locations for referencing the python built-ins.

like image 749
Jason Webb Avatar asked Jan 04 '11 22:01

Jason Webb


1 Answers

Use builtins, or __builtin__ if you're on Python 2.

def open():
    pass

import __builtin__

print open
print __builtin__.open

This gives you:

<function open at 0x011E8670>
<built-in function open>
like image 90
Chris B. Avatar answered Sep 19 '22 12:09

Chris B.