473,395 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Backticks: What up?

I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.

TIA.
Jul 18 '05 #1
6 1567
Steven Brent wrote:
I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.

TIA.


I think `self.data` is equivalent to repr(self.data).

-Don

Jul 18 '05 #2
Backticks are equivalent to repr(). For any x:

`x` == repr(x)

Nothing to do with the return statement.

-- David Goodger
Jul 18 '05 #3
Steven Brent wrote:
I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.


The above is equivalent to

return "Here's the result: %r" % self.data

Note that someone doing the `self.data` thing might really
have wanted to do the string representation instead (though
the two are often the same), so

return "Here's the result: %s" % self.data

would be more appropriate.

-Peter
Jul 18 '05 #4
Steven Brent wrote:
I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.


I was looking for the proper language ref but sometimes they
can be hard to track down:

http://docs.python.org/ref/string-conversions.html

-Peter
Jul 18 '05 #5
Thanks! I didn't know you could use printf type syntax with return....
Jul 18 '05 #6
Steven Brent wrote:
Thanks! I didn't know you could use printf type syntax with return....


The "return" statement allows an arbitratu expression as its target,
even complex objects like sequences, classes and instances. For example:
def tuplify(a, b): .... return a, b
.... tuplify([1, 2], [3, 4]) ([1, 2], [3, 4]) thing, thong = tuplify("sing", (1, 2, 3))
thing 'sing' thong (1, 2, 3)


regards
Steve
Jul 18 '05 #7

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

Similar topics

6
by: Jamie Wright | last post by:
Hey there, I've been searching for a way to add up a number of numbers from a MySQL database and have come up with the SUM() statement, but I'm not really too sure how to use it. The manual...
37
by: Marek Kotowski | last post by:
I am looking for a tool to execute PHP scripts entered in a HTML form. For example: user is given a problem to solve. He writes a PHP script, sends it to a server, where it is executed and...
1
by: lkrubner | last post by:
All the timestamps in my code are wrong by 90 minutes. date INT(11), I'd like to add 5400 to every date field in my database. Rather than do this in PHP, I'm wondering if MySql has a command...
13
by: mensanator | last post by:
I just installed Python 2.3 (upgrading from 2.1). Version 2.3 is noticably faster and the automatic conversion to long integers is very handy: >>> print 2**64 18446744073709551616 But if...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
0
by: Kristina Clair | last post by:
Hi, I have a perl script running suid root (thus running in taint mode), and I'm trying to execute a shell command. Usually I do this using backticks so I can get the output, and usually it is...
4
by: MARTIN DAVIES | last post by:
I hope you can help. My Linux MYSQL 3.23.52 database server is working OK generally. However, in a database I have a field name that ends with a # (hash UK as opposed to £ which is a pound UK)...
2
by: Bart Van der Donck | last post by:
Hello, I am using MySQL 4.0. Say that I have a table named "mytable" having a column "ID" and a column "test columnname". When dumping: mysqldump --opt DATABASE -uUSER -hHOST -pPASS >...
1
by: vans3crown | last post by:
I have to write a Perl script that will be run by submission to an LSF queue: bsub -q somequeue perl myscript.pl If I use exec / system / backticks inside of this script, will all subprocesses...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.