[Tutor] Here's something to talk about (Weidner, Ronald)

W W srilyk at gmail.com
Wed Apr 15 20:14:55 CEST 2009


On Wed, Apr 15, 2009 at 12:27 PM, Carnell, James E <
jecarnell at saintfrancis.com> wrote:

> Since # the list seems thick with OOP questions at the moment, I thought
> this might # be relevant.  Digest and enjoy.
>
> class Item ( object ):
>
>    def __init__( self ):
>        self._FullName = ''
>        self._Recovery = 0
>        self._Exporter = SimpleItemExporter (); # <----? Don't
> understand
>
> Bummer, I was hoping to consider myself at the tip of intermediate
> python programming <sigh>...
>
> This is the first time I have ever seen a variable set to what appears
> to be a function address(?). Since I am at work I can't copy paste this
> thing yet. Is SimpleItemExporter from the parent class, object? I am
> assuming Item extends or inherits (or whatever the right "word" is) from
> object.


First off, the semicolon is probably a syntax error.

SimpleItemExporter is probably a class, but I don't think object is the
parent. If you were trying to access something from object I'm fairly
certain you'd use the dot operator, so it would be
object.SimpleItemExporter()

But if it's a class it would be a new instance of that class. Otherwise it
doesn't make a lot of sense to have the parenthesis (unless
SimpleItemExporter returns something useful... which it isn't named like it
should).

Consider the following:
In [1]: def foo():
   ...:     print "Hello"
   ...:
   ...:

In [2]: x = foo()
Hello

In [3]: x

In [4]: x('a')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/wayne/<ipython console> in <module>()

TypeError: 'NoneType' object is not callable



In [5]: class foo:
   ...:     def __init__(self, spam):
   ...:         print spam
   ...:
   ...:

In [7]: x = foo('eggs')
eggs

In [8]: x
Out[8]: <__main__.foo instance at 0x9f0304c>

In [9]: x()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/wayne/<ipython console> in <module>()

AttributeError: foo instance has no __call__ method

In [10]: x.__init__('eggs')
eggs



--and---

In [11]: class spam:
   ....:     def __init__(self, eggs):
   ....:         lay(eggs)
   ....:     def lay(x):
   ....:         print x
   ....:
   ....:

In [13]: class foo(spam):
   ....:     def __init__(self, bar):
   ....:         lay(bar)
   ....:
   ....:

In [14]: x = foo('Ni')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/home/wayne/<ipython console> in <module>()

/home/wayne/<ipython console> in __init__(self, bar)

NameError: global name 'lay' is not defined


---------------------
The above snippets seem to show what I stated before - SimpleItemExporter is
a class on it's own - not a method/function in the object class. If that
were the case then my class foo should have direct access to the lay
function, AFAIK.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090415/67dd8f80/attachment.htm>


More information about the Tutor mailing list