471,330 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

cls & self

hi,

is cls & self the same thing?

I have seen something like

class A:
def dosomething(cls):
#doing something

How is cls & self differ? How is it use?

Thanks
james

Jul 26 '07 #1
3 21320
On Thu, 26 Jul 2007 03:07:56 +0000, james_027 wrote:
hi,

is cls & self the same thing?

I have seen something like

class A:
def dosomething(cls):
#doing something

How is cls & self differ? How is it use?

Thanks
james
First, you have to understand that the choice of this name is *completely
arbitrary*. You could call it self, cls, this, bogus, helloworld or
whatsoever. The important thing is just: The first argument is (by
default) the instance.

Amongst python developers, many things aren't enforced by the language
(eg. implicit `this` referencing the instance, as in other languages) but
by conventions. It's just way more convenient to call it `self` always.
We call it `cls` when speaking about classmethods. Your method there
might have been headed by a line containing ``@classmethod``.

See http://docs.python.org/lib/built-in-funcs.html#l2h-16 for classmethod
and http://docs.python.org/tut/node11.ht...00000000000000
for this conventional stuff (paragraph "Often, the first argument ...").

HTH,
Stargaming
Jul 26 '07 #2
james_027 schrieb:
hi,

is cls & self the same thing?

I have seen something like

class A:
def dosomething(cls):
#doing something

How is cls & self differ? How is it use?
cls and self are just names. And you most certainly haven't seen the
above, but this instead:

class A(object):
@classmethod
def dosomething(cls):
pass
The decorator "classmethod" makes a method of a class a class-method.
That means that it's not invoked like this

a = A()
a.dosomething()

but

A.dosomething()

instead. Common usage are e.g. factory methods. And because it is a
classmethod, it gets the class passed as first argument, not the instance.
Diez
Jul 27 '07 #3
On 7/27/07, Diez B. Roggisch <de***@nospam.web.dewrote:
james_027 schrieb:
hi,

is cls & self the same thing?

I have seen something like

class A:
def dosomething(cls):
#doing something

How is cls & self differ? How is it use?

cls and self are just names. And you most certainly haven't seen the
above, but this instead:
Quite likely he has seen this, except there followed many other lines and then
dosomething = classmethod(dosomething) at the end.

This disjoint between where it's declared (and looks like a method)
and where it's set to a classmethod was a primary factor behind the
decorator syntax, of course.
Jul 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by RJS | last post: by
1 post views Thread by C. Titus Brown | last post: by
7 posts views Thread by Kirt | last post: by
4 posts views Thread by Mark Rae | last post: by
3 posts views Thread by Skink | last post: by
9 posts views Thread by find clausen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.