NEWBIE: What's the instance name?

R

Raymond Hettinger

[Tim Roberts]
Well, more Pythonish yet is to avoid the extraneous local variables,
avoid the use of a type as a parameter name, and use as many
little-used builtins as possible:

def ishexdigit(char):
return char in string.hexdigits

def ishexstring(strng):
return filter(ishexdigit, strng) == strng

[Uwe Schmitt]
better/faster/more pythonic:

return reduce(lambda a,b: a and ishexdigit(b), strng, True)

In the vague category of Most Pythonic, code using "filter", "lambda",
and "and" tend to be losers. Straight-forward loops run reasonably
fast and are much more clear:

def ishexstring(s):
for c in s:
if c not in '0123456789abcdefABCDEF':
return False
return True


The category of Fastest Code is much less vague but tends towards
using fancy tricks:

def ishexstring(s):
try:
int(s, 16)
except ValueError:
return False
return True


The category of Cutest Code tends to make creative use of data
structures:

def ishexstring(s):
return not sets.Set(s).difference('0123456789abcdefABCDEF')



Raymond Hettinger
 
J

Jeff Epler

The category of Fastest Code is much less vague but tends towards
using fancy tricks:

def ishexstring(s):
try:
int(s, 16)
except ValueError:
return False
return True
True

.... but probably it doesn't fit the OP's definition

Jeff
 
E

Eduardo Elgueta

Michael and the rest,

Your analogy is very good. I think of C++ as an enigmatic and
temperamental cat, and I think of Python as last generation language.
I mean, it's ok if in C/C++ you can't get an instance name (another
reason not to use it). But why not in Python? (I use it a lot)

An object can have many references to it? Ok, give me the name of the
instance I'm using to refer to it:

x.__instance_name__ --> "x"
foo.__instance_name__ --> "foo"

Or the first it finds, if it's not obvious:

self.__instance_name__ --> whatever it finds first
(I'll be carful not to referentiate the instance twice, I promise)

I've never ran into a software design/engineering book saying "storing
an instance name is forbidden under penalty of death."

I don't want to flame anyone. It's just I'm so lazy I wanted to save
myself a parameter in my html generation class, using the instance
name as the html object name:

python:
pass = Input(...)
:
pass.render(page_buffer)

html:
:
<input type="whatever" name="pass" ...>
:

Oh well!

Regards,

Ed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,182
Latest member
LucaCastan

Latest Threads

Top