You can actually do something like it fairly simply, by using the
classmethod decorator, like so:

class Thing:

    @classmethod
    def do_something(cls, some_arg):
        # do something with some_arg

Then you could call it:
Thing.do_something(3)

Notice that you don't pass the "cls" argument--Python does that for
you, in the same way that it passes "self" automatically for instance
methods.  You can also use the staticmethod decorator, which does
essentially the same thing as classmethod, except that it doesn't pass
the class as an argument; I tend to find this less useful in
practice.

-Jeff

On Jun 29, 6:25 pm, Mark Jones <mark0...@gmail.com> wrote:
> Yea, I'm not wanting to use stuff.objects, but I'm wanting to pull
> some of the same voodoo, probably not safe for a python novice like
> myself :-)
>
> On Jun 29, 5:24 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
>
> > On Mon, Jun 29, 2009 at 5:19 PM, Mark Jones <mark0...@gmail.com> wrote:
>
> > > I can't seem to reason out why/how this works.
>
> > > I have a class Named Stuff
>
> > > I can say Stuff.objects.filter(.....) and that will return valid set
> > > of data.
>
> > > What I can't understand is what exactly is objects, and why is it I
> > > can call it with Stuff.objects, but I can't call it with stuff.objects
> > > (an instance of Stuff).
>
> > > >>> dir(Stuff) shows me 'objects'
> > > >>> dir(stuff) shows me 'objects'
>
> > > >>> type(Stuff.objects)
> > > <class 'django.db.models.manager.Manager'>
> > > >>> type(stuff.objects)
> > > Traceback (most recent call last):
> > >  File "<console>", line 1, in <module>
> > >  File "...manager.py", line 151, in __get__
> > > AttributeError: Manager isn't accessible via Stuff instances
>
> > > What is the python Magic going on here to make this possible?
>
> > > I'm asking because I want to make something like 'objects' in that it
> > > doesn't need an instance, but it is scoped within the model of Stuff.
>
> > > My background is C++ and these look like methods/objects that are
> > > static to the class, not part of the instances.  I just can't figure
> > > out how to declare and instantiate them in python.
>
> > Django uses an advanced python feature called descriptors in order to
> > prevent you from accessing a manager (which is what "objects" is) from an
> > instance.  My understanding of the reason for this is somewhat conceptual:
> > asking for all the objects that are "Stuff"s makes sense, but asking for all
> > the objects that are "some object" doesn't make as much sense.
>
> > Alex
>
> > --
> > "I disapprove of what you say, but I will defend to the death your right to
> > say it." --Voltaire
> > "The people's good is the highest law."--Cicero
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to