473,581 Members | 3,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

def index(self):

Is there a difference between

<code>
class HelloWorld:
def index(self):
index.exposed = True
return "Hello world!"
</code>

and

<code>
class HelloWorld:
def index(self):
self.exposed = True
return "Hello world!"
</code>
Dec 18 '06 #1
14 5096
Gert Cuykens kirjoitti:
Is there a difference between

<code>
class HelloWorld:
def index(self):
index.exposed = True
return "Hello world!"
</code>

and

<code>
class HelloWorld:
def index(self):
self.exposed = True
return "Hello world!"
</code>
The resident experts seemingly being absent for a while, I'll strike:

Yes: the first gives a runtime error and the second is OK.
I've renamed the second class to HelloWorld2 and then:
>>hw = HelloWorld()
hw2 = HelloWorld2()
>>hw.index()
Traceback (most recent call last):
File "<pyshell#144>" , line 1, in <module>
hw.index()
File "C:\Python\ Dive into Python\Py\apihe lper.py", line 40, in index
index.exposed = True
NameError: global name 'index' is not defined
>>hw2.index()
'Hello world!'

The error message shows that the Python compiler has interpreted the
construction 'index.exposed' to refer to a global variable 'index' that
doesn't exist at run time. The second class succesfully defines an
instance attribute 'exposed' as can be seen by:
>>print hw2.exposed
True

HTH
Jussi
Dec 18 '06 #2
Gert Cuykens a écrit :
Is there a difference between

<code>
class HelloWorld:
def index(self):
index.exposed = True
return "Hello world!"
</code>

and

<code>
class HelloWorld:
def index(self):
self.exposed = True
return "Hello world!"
</code>
Ask yourself what are the names 'index' and 'self' refering to in the
context of this function, and you'll have the answer. And if you can't
answer, then it may be time to learn Python...

FWIW, the first version raises an exception (unless of course the name
'index' is already bound in the enclosing scope). And the second won't
probably work as expected with CherryPy.

Dec 18 '06 #3
FWIW, the first version raises an exception (unless of course the name
'index' is already bound in the enclosing scope). And the second won't
probably work as expected with CherryPy.
<code>
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True #DOOOOOOH!
</code>

i skipped reading the chapter about 2.1.8 Indentation. Guess how many
hours it took to realize 2 spaces isn't the same as 1 space lol

Any other chapters i should read horizontal instead of vertical for a
php5 htm jso wane be snake user :)
Dec 18 '06 #4
Gert Cuykens a écrit :
>FWIW, the first version raises an exception (unless of course the name
'index' is already bound in the enclosing scope). And the second won't
probably work as expected with CherryPy.


<code>
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True #DOOOOOOH!
And the winner is....
</code>

i skipped reading the chapter about 2.1.8 Indentation. Guess how many
hours it took to realize 2 spaces isn't the same as 1 space lol
Hint:
- use a good code editor with auto(de)indenta tion (emacs + python-mode
is quite nice)
- use 4 spaces for indentation
Any other chapters i should read horizontal instead of vertical for a
php5 htm jso wane be snake user :)
The whole thing, I guess. While Python is quite easy to get started
with, there are a few gotchas. You're above snippet should be:

class HelloWorld(obje ct):
def index(self):
return "Hello World"
index.exposed = True
Dec 19 '06 #5
Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
>
Gert Cuykens a écrit :
>>FWIW, the first version raises an exception (unless of course the name
'index' is already bound in the enclosing scope). And the second won't
probably work as expected with CherryPy.

<code>
class HelloWorld:
def index(self):
return "Hello world!"
index.expose d = True #DOOOOOOH!

And the winner is....
></code>

The whole thing, I guess. While Python is quite easy to get started
with, there are a few gotchas. You're above snippet should be:

class HelloWorld(obje ct):
def index(self):
return "Hello World"
index.exposed = True
Many people find it more readable to write that as:

class HelloWorld(obje ct):
@cherrypy.expos ed
def index(self):
return "Hello World"

I haven't decided yet.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 20 '06 #6
Tim Roberts a écrit :
Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote :
>>Gert Cuykens a écrit :
>>>>FWIW, the first version raises an exception (unless of course the name
'index' is already bound in the enclosing scope). And the second won't
probably work as expected with CherryPy.

<code>
class HelloWorld:
def index(self):
return "Hello world!"
index.expose d = True #DOOOOOOH!

And the winner is....

>>></code>

The whole thing, I guess. While Python is quite easy to get started
with, there are a few gotchas. You're above snippet should be:

class HelloWorld(obje ct):
def index(self):
return "Hello World"
index.exposed = True


Many people find it more readable to write that as:

class HelloWorld(obje ct):
@cherrypy.expos ed
def index(self):
return "Hello World"
So do I. But this breaks compatibility with older Python versions, and
is perhaps a bit confusing for someone that doesn't seem to really get
some basic Python stuffs like what 'self' is...

My 2 cents
Dec 20 '06 #7
class HelloWorld(obje ct):
@cherrypy.expos ed
def index(self):
return "Hello World"
do i have to write @cherrypy.expos ed before every def or just once for
all the def's ? and why not write something like @index.exposed ?

in other words i have no idea what @ actually does i only know i have
too write it to make it work :) Am guessing @ is something like
prototyping in javascript but then def index is not a object but a
method ? So that would not make sense ?

oh and self stands more or less for private method right ?
Dec 20 '06 #8
Gert Cuykens wrote:
class HelloWorld(obje ct):
@cherrypy.expos ed
def index(self):
return "Hello World"

do i have to write @cherrypy.expos ed before every def or just once for
all the def's ? and why not write something like @index.exposed ?

in other words i have no idea what @ actually does i only know i have
too write it to make it work :) Am guessing @ is something like
prototyping in javascript but then def index is not a object but a
method ? So that would not make sense ?

oh and self stands more or less for private method right ?
Ouch. Do you really expect to learn a language from scratch by Q&A in a
malining list ? That's a very inefficient way to spend both yours and
others' time. Do your homework first by reading one of the several free
tutorials online and come back when you have trouble with something
specific you didn't understand.

George

Dec 21 '06 #9
"George Sakkis" <ge***********@ gmail.comwrote:
Gert Cuykens wrote:
class HelloWorld(obje ct):
@cherrypy.expos ed
def index(self):
return "Hello World"

do i have to write @cherrypy.expos ed before every def or just once
for all the def's ? and why not write something like @index.exposed ?

in other words i have no idea what @ actually does i only know i have
too write it to make it work :) Am guessing @ is something like
prototyping in javascript but then def index is not a object but a
method ? So that would not make sense ?

oh and self stands more or less for private method right ?

Ouch. Do you really expect to learn a language from scratch by Q&A in
a malining list ? That's a very inefficient way to spend both yours
and others' time. Do your homework first by reading one of the several
free tutorials online and come back when you have trouble with
something specific you didn't understand.
To be perfectly fair, searching for information about decorators if you
don't know what they are called is kind of hard. Searching for
information about the '@' character in the Python documentation doesn't
lead to much. The CherryPy tutorial does at least mention this
decorator, just not in any meaningful way:
Exposing objects

CherryPy maps URL requests to objects and calls the suitable method
automatically. The methods that can be called as a result of external
requests are said to be exposed.

Objects are exposed in CherryPy by setting the exposed attribute. This
an be done directly on the object itself: object.exposed = True.
Methods can also be exposed using a special decorator:

Why this distinction?

The distinction between published and exposed objects may seem silly
or unnecessary at first. By definition, all exposed objects are also
published, so why do we need to care about non-exposed objects?
To the OP:

do i have to write @cherrypy.expos ed before every def or just once for
all the def's ?
Before every def that you wish to expose, which won't be all of them.
and why not write something like @index.exposed ?
because that isn't how it works. cherrypy.expose d is a decorator, which
means it is a function that takes a function as an argument modifies it in
some way and returns the modified function as its result.
>
in other words i have no idea what @ actually does i only know i have
too write it to make it work :) Am guessing @ is something like
prototyping in javascript but then def index is not a object but a
method ? So that would not make sense ?
@expr
def fn(...): ...

is exactly equivalent to:

def fn(...): ...
fn = (expr)(fn)

except that at the point when the decorator is called the function has not
yet been assigned to the name 'fn'. (Also remember that def is an
executable statement so it happens when the line containing the def is
execfuted, and that names assigned to functions are just like other
variable names and can be rebound at will.)
>
oh and self stands more or less for private method right ?
if you have to ask that question about 'self' you really do need to read
some introductory texts on Python.

Methods get passed their instance as their first parameter (usually this is
implicit in the call, but it is always explicitly shown in the method) and
self is simply the conventional name. Many other languages use 'this'
instead of self and make the passing of 'this' implicit inside the method
as well as outside. There are good reasons why it is explicit in Python,
but just remember that the first parameter a method receives is always the
instance and you won't go far wrong.
Dec 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
13012
by: aurora | last post by:
This may sound a little crazy. I capture the output of one class by redirecting the sys.stdout. However the is another threading running at the same time and occasionaly it output some messages to the redirected sys.stdout irreleveant to the output I want to capture. Is there a way to redirect output specific to some threads? aurora
9
2130
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a generator and do a or any other kind of slice it reads all the values up to 100 also. I know a lot of generators have to do all previous parts...
4
2477
by: techiepundit | last post by:
I'm a Python newbie who just started learning the language a few weeks ago. So these are beginner questions. I have a list of sockets that I use for select.select calls like this: ReadList,WriteList,EventList = select.select(self.SocketList,,,3) In parallel with that list of sockets I want something that is like a list of...
35
29204
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. import shutil, os, sys
5
2642
by: vd12005 | last post by:
Hello, While playing to write an inverted index (see: http://en.wikipedia.org/wiki/Inverted_index), i run out of memory with a classic dict, (i have thousand of documents and millions of terms, stemming or other filtering are not considered, i wanted to understand how to handle GB of text first). I found ZODB and try to use it a bit, but i...
3
4291
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: self.fonts=list(tkFont.families()) self.fonts.sort() for item in self.fonts: self.fontlist.insert(END, item) #self.fontlist is the
122
5455
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was the Sequence Types page of the Library Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search of the Library Reference's...
6
2176
by: xkenneth | last post by:
Looking to do something similair. I'm working with alot of timestamps and if they're within a couple seconds I need them to be indexed and removed from a list. Is there any possible way to index with a custom cmp() function? I assume it would be something like... list.index(something,mycmp) Thanks!
3
2411
by: Riccardo Murri | last post by:
Hello, I have some code that stops when trying to find a graph in a list of similar graphs:: (Pydb) list 110 try: 111 canonical = self.base 112 except ValueError: 113 raise ValueError, \
0
7886
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...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7920
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...
0
6569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
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...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2312
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
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
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...

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.