[Tutor] Languages, was Which programming language is better

Kirby Urner urnerk@qwest.net
Thu, 28 Mar 2002 15:13:18 -0800


At 02:52 PM 3/28/2002 -0800, Sean 'Shaleh' Perry wrote:

> > What would the long way look like?  Are foo and bar
> > properties of my_really_long_name object?  Then why
> > not go:
> >
> > o = my_really_long_name
> > o.foo = 1
> > o.bar = 2
> >
>
>Sure, I could do that.  But that feels like a hack to me.

Well, it doesn't involve inventing any new syntax
i.e. with...end blocks -- so in a way it's simpler.

>Besides, does o.foo = 1 also set my_realy_long_name.foo to 1?

Yes of course, if my_really_long_name is an object,
then o = my_really_long_name just sets up an alternative
pointer to the same object.  Not a copy or anything.

  >>> class Test:
         pass

  >>> myreallylongname = Test()
  >>> o = myreallylongname
  >>> id(o)
  10939472
  >>> id(myreallylongname)  # same id means same object
  10939472

>What if these are attributes which call functions?
>

Well, if they're class methods, then you probably don't
want to obliterate them by making them integers --
it's up to you.

  >>> class Test:
          def f(self):
             return "!"


  >>> myreallylongname = Test()
  >>> o = myreallylongname
  >>> o.f()
  '!'
  >>> o.f = 1
  >>> o.f()
  Traceback (most recent call last):
    File "<pyshell#14>", line 1, in ?
      o.f()
  TypeError: 'int' object is not callable

But you can certainly use the reassignment "hack" to
shorten the typing needed to invoke methods, as shown
by o.f() -> '!' above.

Kirby