473,507 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21559
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2971
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
1
2054
by: flupke | last post by:
Hi, i'm trying to convert my java console app to a python gui. Now, the only problem i seem to have at the moment are the resizers for the layout. It seems that for the purpose of what i'm...
2
2580
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
1
1977
by: C. Titus Brown | last post by:
Hi all, while playing with PBP/mechanize/ClientForm, I ran into a problem with the way htmllib.HTMLParser was handling encoded tag attributes. Specifically, the following HTML was not being...
7
3010
by: Michael Galvin | last post by:
I am trying to use Python to send to the printer a calender filled with a mix of text and simple graphics. I want to draw on the printed page something like a table with 6 rows and 7 columns to...
7
1784
by: Kirt | last post by:
i have walked a directory and have written the foll xml document. one of the folder had "&" character so i replaced it by "&amp;" #------------------test1.xml <Directory> <dirname>C:\Documents and...
4
1469
by: Mark Rae | last post by:
Hi, Is there a way to prevent ASP.NET 2 from stripping off the self-closing meta tags...? E.g. <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1" /> is turned into
3
3184
by: Skink | last post by:
Hi, I created simple property classes with editing option, but since i'm not too much experienced in PyQt4 i'd like to ask if i handle ColorProperty changing right. Any other Property has its...
9
1737
by: find clausen | last post by:
How can I use this: if (!zxmes && self.name != "menu") and add if (zmes == 1) if (!zxmes && self.name != "menu" || zmes == 1) and make it work.
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.