Noob questions about Python

2007-10-17 Thread Ixiaus
I have recently (today) just started learning/playing with Python. So
far I am excited and impressed (coming from PHP background).

I have a few questions regarding Python behavior...

val = 'string'
li = list(val)
print li.reverse()

returns nothing, but,

val = 'string'
li = list(val)
li.reverse()
print li

returns what I want. Why does Python do that?

Also I have been playing around with Binary math and noticed that
Python treats:

val = 00110

as the integer 72 instead of returning 00110, why does Python do that?
(and how can I get around it?)

Grateful for any replies!

-- 
http://mail.python.org/mailman/listinfo/python-list


Noob questions about Python

2007-10-17 Thread Ixiaus
I have recently (today) just started learning/playing with Python. So
far I am excited and impressed (coming from PHP background).

I have a few questions regarding Python behavior...

val = 'string'
li = list(val)
print li.reverse()

returns nothing, but,

val = 'string'
li = list(val)
li.reverse()
print li

returns what I want. Why does Python do that?

Also I have been playing around with Binary math and noticed that
Python treats:

val = 00110

as the integer 72 instead of returning 00110, why does Python do that?
(and how can I get around it?)

Grateful for any replies!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Noob questions about Python

2007-10-17 Thread Ixiaus
Thank you for the quick responses.

I did not know that about integer literals beginning with a '0', so
thank you for the explanation. I never really use PHP except for
handling basic forms and silly web stuff, this is why I picked up
Python because I want to teach myself a more powerful and broad
programming language.

With regard to why I asked: I wanted to learn about Binary math in
conjunction with Python, so I wrote a small function that would return
a base 10 number from a binary number. It is nice to know about the
int() function now.

Just for the sake of it, this was the function I came up with:

def bin2dec(val):
li = list(val)
li.reverse()
res = [int(li[x])*2**x for x in range(len(li))]
res.reverse()
print sum(res)

Now that I look at it, I probably don't need that last reverse()
because addition is commutative...

def bin2dec(val):
li = list(val)
li.reverse()
res = [int(li[x])*2**x for x in range(len(li))]
print sum(res)

It basically does the same thing int(string, 2) does.

Thank you for the responses!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Noob questions about Python

2007-10-17 Thread Ixiaus
> Right idea: now to remove all those intermediate lists you construct.
> 1. reversed(val) creates an iterator that runs over the elements (here
> of a string) in reverse order.
> 2. enumerate() is usually better than using an explicit list index.
> 3. You can use a generator in your sum to avoid constructing the final
> list.
>
> Applying these to your function, and noting that n << k is nicer than
> n * 2 ** k, we get a one-liner:
>
> def bin2dec(val):
> return sum(int(i) << k for k, i in enumerate(reversed(val)))
>
> Or a slightly nicer alternative is to filter the generator using 'if':
>
> def bin2dec(val):
> return sum(1 << k for k, i in enumerate(reversed(val)) if int(i))
>
> --
> Paul Hankin

Thank you for this reply, I only wish I could come up with functions
so elegantly refined!

I know '<<' is shifting x over by n bits; but could you point me to
some literature that would explain why it is the same as "x*2**n"?
(Googling only returns bit shift, but doesn't necessarily explain the
manner in which you are using it)

I will have to read up more on Generators, but maybe you can give me a
lowdown on why
sum([1 << k for k, i in enumerate(reversed(val)) if int(i)]) is less
efficient than using a Generator (is the generator a 'temporary'
list?)
sum(1 << k for k, i in enumerate(reversed(val)) if int(i))

-- Parnell Springmeyer

-- 
http://mail.python.org/mailman/listinfo/python-list


Noob: Loops and the 'else' construct

2007-10-18 Thread Ixiaus
I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.

At first glance I interpreted it as being a bit like the 'default'
structure in PHP's switch block... But the switch block isn't a loop,
so, I am now confused as to the reason for using 'else' with the for
and while loops...

A few quick tests basically show that statements in the else structure
are executed at the fulfillment of the loop's expression (ie, no
break).

Example:

for i in range(10):
 print i
else:
 print 'the end!'

0
1
2
3
4
5
6
7
8
9
10
the end!

-- 
http://mail.python.org/mailman/listinfo/python-list


Noob: Loops and the 'else' construct

2007-10-18 Thread Ixiaus
I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.

At first glance I interpreted it as being a bit like the 'default'
structure in PHP's switch block... But the switch block isn't a loop,
so, I am now confused as to the reason for using 'else' with the for
and while loops...

A few quick tests basically show that statements in the else structure
are executed at the fulfillment of the loop's expression (ie, no
break).

Example:

for i in range(10):
 print i
else:
 print 'the end!'

0
1
2
3
4
5
6
7
8
9
10
the end!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Noob questions about Python

2007-10-18 Thread Ixiaus
That was a very helpful answer Steven, thank you for taking the time
to write it!

-- 
http://mail.python.org/mailman/listinfo/python-list


Python equivalent to PHP's SPL __autoload() ??

2008-04-27 Thread Ixiaus
I was curious (and have spent an enormous amount of time on Google
trying to answer it for myself) if Python has anything remotely
similar to PHP's SPL __autoload() for loading classes on the fly??

After digging through docs I feel doubtful there is such a language
feature, but, it is possible I missed something or maybe someone has
written an extension?!?

Thanks in advance!
--
http://mail.python.org/mailman/listinfo/python-list