On Mon, 13 Oct 2008 08:56:34 -0700 (PDT), azrael <ju*********@gm ail.comwrote:
>You know, sometimes it annoys me to write a for loop in Python. If we use a list a=[1,2,3,4], and want to loop through it, Python offers the next option
>>>>for i in a: print i
1 2 3 4
I love this. So simple and smooth. But what happens if we need also the position of an object in a list. Then there comes this annoying writing.
>>>for i in range(len(a)): print a[i], i
1 0 2 1 3 2 4 3
I think that it would be great if the Python language, which is a totaly OOP language, could also use the index variable from the first example and consider it as an object with a Object variable. I mean the following.
>>>>for i in a: print i, i.index # i.index is my sugesstion
1 0 2 1 3 2 4 3
I think that this would be great and we cou pass by this annoying "range(len(a)) " functions
Adding a random new attribute to arbitrary objects any time they happen
to end up in a for loop would be catastrophicall y terrible. Consider
the enumerate builtin, instead.
>>for i, e in enumerate('abcd '):
.... print i, e
....
0 a
1 b
2 c
3 d
Jean-Paul 6 988
I know that. enumerate is a great function. But this way it always
adds some complexity. I think that it is more better to give a man a
better tool then to let him play with a not so good one. People like
Python because of his simplicity in comparison with c++. Maybe People
would like him even more it would be a bit more simple but the same
powerfull.
azrael wrote:
I know that. enumerate is a great function. But this way it always
adds some complexity. I think that it is more better to give a man a
better tool then to let him play with a not so good one. People like
Python because of his simplicity in comparison with c++. Maybe People
would like him even more it would be a bit more simple but the same
powerfull.
But the added complexity of your approach of setting an attribute at runtime
that can
- add arbitrary costs (who knows that setting the attribute might not
invoke a RPC-call?) for *every* enumeration of a list
- conflict with multithreaded access or even only cartesian products of the
same list of objects, rendering it simply useless
is *less* hassle than putting a simple enumerate in place, just when needed?
You sure have an interesting view of complexity. Not exactly text-book-based
though.
Diez
At 2008-10-13T16:11:26Z, azrael <ju*********@gm ail.comwrites:
I know that. enumerate is a great function. But this way it always
adds some complexity. I think that it is more better to give a man a
better tool then to let him play with a not so good one. People like
Python because of his simplicity in comparison with c++. Maybe People
would like him even more it would be a bit more simple but the same
powerfull.
"enumerate" doesn't add complexity: it adds specificity. When you use it,
there is no question of intent. You are saying, explicitly, that you're
going to want to use the index.
Your way adds an enormous amount of complexity in that it's magic behavior
with no obviously good implementation. What if the objects in my list
already have an attribute named "index" and I don't want your loop
contstruct to overwrite it? What if I never use enumerate as it is and
don't want to pay for the extra overhead of modify every object I'll ever
get from an iterator?
Thanks, but no. One of the things I love about Python is its lack of magic.
--
Kirk Strauser
The Day Companies
azrael wrote:
I know that. enumerate is a great function.
Agreed. Enumerate works everywhere an iterable is wanted.
Just like other functions that produce iterables.
A special trick for for-loops would only work for for-loops.
azrael wrote:
I know that. enumerate is a great function. But this way it always
adds some complexity.
That objection doesn't really make sense. Your suggestion is far more
complex: It requires that an `index` attribute be added to every
element in an iteration. What's the lifetime of that attribute? Is it
a real attribute, permanently attached to the item? What about for
objects that already have an `index` attribute? If it's a "virtual"
attribute, how long does it last? Is the iterated object a
newly-created wrapper object for the purposes of providing that
attribute? If so, how do you make that more efficient than just
creating a new object for each object you iterate over? And so on.
Just use `enumerate`. That's what it's there for.
I think that it is more better to give a man a
better tool then to let him play with a not so good one. People like
Python because of his simplicity in comparison with c++. Maybe People
would like him even more it would be a bit more simple but the same
powerfull.
The problem is your suggestion would make Python a worse tool, not a
better one.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Triumph cannot help being cruel.
-- Jose Ortega y Gasset This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian van den Broek |
last post by:
Hi all,
There have been a few posts over the last month or so expressing a bit
of exasperation with the "rising tide of newbie's". (Or, more
accurately, the rising tide of questions from newbie's not trying to
follow ESR's advice.)
A month or so ago (in a thread found here: http://tinyurl.com/5bj8j), I
suggested that it might help the situation if there were a python.org
wiki page that amounted to a "Welcome to the Python community;...
|
by: Stewart Midwinter |
last post by:
I'm starting up a new website for a small community of local sports
enthusiasts. I want to be able to create pages of information easily
(i.e. a content management system), but also allow for discussion on
multiple topics (i.e. a forum). Numerous people should be able to log
in and create or update content, under the guidance of an admin
naturally.
Call me perverse, but I don't want to use phpBB for the forum. I'm
not seeing anything...
|
by: Yoni Rapoport |
last post by:
Hy everyone, just wanted to share a thought regarding UI.
If you really think about it, events such as "Click", "DoubleClick",
"KeyDown", "KeyUp", etc... (which clutter every winforms control we see) are
mostly annoying and sometimes even harmful.
How does knowing a control was clicked by the mouse (and how many times it
was clicked) help a client object know what the user intended to do by that
action?
How many times have you found...
|
by: Andrew Ducker |
last post by:
Almost every project I work on, I end up with some variant of an
IResettable interface - so that controls can be reset to a default
value. Controls have a ResetText method, but that's not much use for
checkboxes, and you can't set a default for things to be reset back to,
on a control by control basis.
Does anyone else find themselves using similar functionality? Because
if there's enough of us, it might be something we can push for in...
|
by: |
last post by:
I need source control to serve just one developer: me. Can someone suggest a
system that would suit my needs? I am the only person writing code, and I
manage the servers. Many of the SC solutions out there seem premised around
the needs of a large team and coordinating their work. That's not me.
Data points that might help: I'm on MSDN Universal. My environment is VS.NET
2005, multiple Windows 2003 Server boxes with replicated...
| |
by: Bruce One |
last post by:
Well folks, considering the high prices for the current VSTS MSDN Premium
subscription, I propose we take a pause and look of what each one of the 3
possible package DOES NOT have:
Team Edition for Software Architects: Unit Testing, Code Coverage / Dynamic
Code Analyzer, Static Code Analyzer, Code Profiler / Load Testing, Manual
Testing, Test Case Management, and Web Testing
Team Edition for Software Developers: Application Designer,...
|
by: ADezii |
last post by:
If you want to visit each item in an Array, you have two alternatives:
Use a For Each..Next loop, using a Variant to retrieve each value in turn.
Use a For...Next loop, looping from the Lower Bound to the Upper Bound of the Array.
For Each...Next seems simpler because you need not worry about retrieving the Lower and Upper Bounds- the loop takes care of that for you.
For Each varValue In alngValues
j = varValue
Next varValue...
|
by: Mikhail Kovalev |
last post by:
Hi.
I work with recursive array trees of 10-20 levels in PHP. So far I
have been using serialize() to store the arrays, generating files 5+
MB large, which take 10-15 seconds to unserialize and about tenfold of
RAM! I've been adviced to use MySQL (relational db's), but are there
any other options beside that and var_export/include? Something that
works in a similar way as MySQL when adding a new element without
loading the whole...
|
by: azrael |
last post by:
You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=, and want to loop through it, Python offers the
next option
1
2
3
4
I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |