[Chicago] New member and introducing myself.

Brian Herman brianherman at gmail.com
Sun Oct 5 04:00:20 CEST 2014


Wow that was really great!

On Fri, Oct 3, 2014 at 11:55 PM, Jordan Bettis <jordanb at hafd.org> wrote:

>  On 10/03/2014 05:05 PM, Lewit, Douglas wrote:
>
>   Quick question here.  In my Java course we just got done learning about
> static methods and non-static methods (although I'm still a little confused
> about their difference ).  I know that Python can be programmed using the
> OOP paradigm.  Does Python also have static methods and non-static methods?
>
>
> Think about it like this: You can define a class:
>
> class C(object):
>     @staticmethod
>     def s():
>         print "I'm static method"
>     def i(self):
>         print "I'm an instance of {}".format(repr(self))
>
> You can instantiate an object of type C:
>
> o = C()
>
> And then you can call your methods:
>
> o.s()
> I'm static method
>
> o.i()
> I'm an instance of <__main__.C instance at 0x7f607d8f8cf8>
>
> Now here's the important bit: the class you defined (C) *is itself an
> object*, you can pass class definitions around your program and treat them
> just like any other object:
>
> def print_an_object(obj):
>     print obj
>
> print_an_object(C)
> __main__.C
>
> Suppose you want to bundle some functionality with the class definition,
> but you want it to be available *without creating an instance* of that
> Class, but just by accessing it through the class definition object. You
> can't do that with a regular method, since it takes "self" it wants an
> instance:
>
> C.i()
> TypeError: unbound method i() must be called with C instance as first
> argument (got nothing instead)
>
> But you can do that with a static method. Since the static method doesn't
> take a "self" parameter you can just call it the way you'd call any
> function in python, directly from the definition:
>
> C.s()
> I'm static method
>
> As it turns out static methods aren't terrificly useful in python but they
> can be nice occasionally for keeping things organized. A much more useful
> construct is the class method:
>
>  class C(object):
>     @classmethod
>     def cw(cls):
>         print cls
>
> Class methods don't take "self" as their magic parameter, instead they
> take the *class definition* as that parameter. They can be called directly
> from the class definition like a static method, and they can also be called
> from an instance, but in either case they end up getting passed the class
> definition:
>
> C.cw()
> <class '__main__.C'>
>
> o = C()
> o.cw()
> <class '__main__.C'>
>
> These functions can be really useful for, for instance, creating factory
> methods that produce custom versions of C instances.
>
> The most important conceptual thing to understand is that the *class
> definition is itself an object.* If that doesn't make sense think about it
> until it does. If it does make sense think about the implications of it and
> then the difference between static methods, instance methods and class
> methods will be clear.
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>


-- 


Thanks,
Brian Herman
kompile.org <http://www.kompile.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20141004/2de684bd/attachment.html>


More information about the Chicago mailing list