473,666 Members | 2,080 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 21565
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 "classmetho d" 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.w eb.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(dos omething) 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
2983
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 "import numarray" allows the exe to run. Left in, I get "4.exe has generated errors" etc. I'm going around and around and there isn't much on Google. py2exe output is last.
1
2063
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 trying to do, specifying the coordinates is easier that fighting with the layout resizers. 1) I have a screen split in 2. Left side is a textcontrol where logging will end up. All text will be appended to the textcontrol. Ideally this should allow...
2
2593
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 of classes that allow me to interact with network devices. In other words, if I have a path and filename inside of a string variable in python, how do I send the file to the printer?? Thanks in advance
1
1985
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 handled correctly: <option value="Small (6&quot;)">Small (6)</option> The 'value' attr was being given the escaped value, not the
7
3023
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 represent a calendar. I want to place text precisely within those boxes on the printed page. I am using Python 2.4 on Windows XP I was in the past able to do this within Visual Basic using its printer object. Visual Basic's printer object uses...
7
1792
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 Settings\Administrator\Desktop\1\bye w&amp;y </dirname> <file> <name>def.txt</name> <time>200607130417</time> </file>
4
1476
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
3197
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 own editor and their control flow is imho ok. Hovewer i'm not sure about ColorProperty. What i do is: in createEditor(self, parent, option, index) i call QtGui.QColorDialog.getColor() and return None
9
1746
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
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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 we have to send another system
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.