jump to navigation

There’s always an easier way March 8, 2007

Posted by sdpurtill in Python.
trackback

I am no beginner in Python, but I’m definitely no guru. So I still have problems expressing some things. It’s like learning Spanish — you know enough of the language for others to understand you, but there is always an easier way to convey what you want to say. I know enough Python for the computer to understand me, but here is a situation that just came up that shows there’s always an easier, shorter way to do things.

def _get_custodian_clients(self):
  "Returns all the clients of the custodian"
  str = ''
  i = 1
  total_cli = self.custodian_total_clients
  for cli in self.clients.all():
    str += cli.name
    i += 1
    if i <= total_cli:
      str += ' | '
  return str

I knew there had to be an easier way to do it, but I just couldn’t figure it out. So I asked people that knew more than me, i.e., the guys on the #django IRC Channel. Two minutes later, and I have a nice, elegant, even readable solution.


def _get_custodian_clients(self):
  "Returns all the clients of the custodian"
  return ' | '.join([client.name for x in self.clients.all()])
custodian_clients = property(_get_custodian_clients)

Sometimes I am amazed by my stupidity.

Advertisements

Comments»

1. emote - March 8, 2007

Ahahahahahahaha, Sam Purtill, be grateful you aren’t actually stupid. Otherwise I would never hang out with you. I hate stupid people. People who are literally retarded actually have an excuse.

2. Test - March 30, 2007

Hi all!

Bye


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: