name of object inside it's methods?

Doug Stanfield DOUGS at oceanic.com
Mon Feb 28 14:24:44 EST 2000


[Michal Wallace (sabren) wrote:]
> > > B) you could give the object a .Name attribute and just
> > >    use that instead. :)

[Moshe Zadka said:]
> > class foo():
> > 	pass
> > b = foo()
> > b.Name = "b"
> > a = b
> > print a

[Gerrit Holl said:]
> <nitpick>
>   File "<stdin>", line 1
>     class foo():
>               ^
> SyntaxError: invalid syntax
> </nitpick>

What seems to work is:

class foo():
    pass
b = foo()
b.Name = "b"
print b.Name
# Presuming the original poster didn't want the behaviour
# implied by Moshe's post, the following is more appropriate:
a = foo()
a.Name = "a"
print a.Name

# Isn't this the kind of thing one would normally do?  Perhaps
# what was wanted?
c = foo()
c.Type = "WIDGETTHINGY"
c.Name = "ThisOne"
d = []
d.append(c)
for wdgt in d:
    print "%s is type %s" % (wdgt.Name,wdgt.Type)

-Doug-




More information about the Python-list mailing list