[Tutor] Lost, and Asking for Dir()-ections
Wesley J. Chun
Sat, 12 Feb 2000 03:31:03 -0800 (PST)
> Date: Thu, 10 Feb 2000 08:58:15 -0600
> From: "Curtis Larsen" <curtis.larsen@Covance.Com>
> Probably a silly question, but I'm new to this object-oriented stuff.
aren't we all?!? ;-)
> When doing a dir(object) to see what functions/methods are available in
> a script, how do I know if something is a function or a method? I ask
> because after I use dir() on a script, I try doing a
> "scriptname.functionname() to call up the likely thing I want to do --
> "urllib.test()" for example.
right. when you import a module and do a dir(module),
you get a list of valid attributes for that module,
whether it be a variable or function.
you had another question up there, regarding the difference
between a function and a method. to put it simply, a
method is just a function defined for a class. other than
that, functions and methods are pretty much the same, so
i'm not sure what you're asking there.
i think what you want to say is, "how can i tell if a module's
attribute is just a variable or a function that i can call?"
vars() is like an extended version of dir(). it shows not
only what the attributes are, but what their values are; and
from that, it is pretty easy to deduct what types they are.
> I tried doing this with a recent script I picked up, and after doing the
> same thing ("scriptname.blah()"), it returned "<scriptname.objectname
> instance at memorylocation>". It wouldn't actually do what I was
> looking for until I gave it an equation such as "x=scriptname.blah()".
> Then an "x" object was created with property values, etc., that
> contained what I was looking for.
what you did there is you accessed a class. now this may
be confusing, but taking a class and making an instance out
of it works the *same way* as calling a function... but you
are not really doing that... it just *looks* like it.
if foo is a function, you would use foo() to call it right?
but if MyClass was a class, you would use MyClass() to create
an instance (object) of that class! looks *are* deceiving!!
when you did module.MyClass() standalone above, you created
an instance of a class, but did not assign it to any variable,
so the interpreter just spit the instance back at you, hence
the <module.Class instance at XXXXX>.
when you did x=module.MyClass(), it correctly created the
instance for you, but because you assigned it to a variable,
the interpreter did not spit out that message. i can guar-
antee you that if you did typed 'x' by itself right after
you did the x=module.MyClass(), it would give you the exact
same message: <module.Class instance at XXXXX>.
> How would I recognize the need for using this equation notation in the
> future? (What's the diff?)
from the above, we can see that you did two different things:
1) called a function in a module, i.e. urllib.test()
2) created a class instance (which also uses the "functional
notation")... it appears you were calling a function when
that's the difference. the first example had nothing to do
with classes, so this "equation notation" was not necessary
because you were not creating an object. (now it *is* pos-
sible that the function you execute returns an object to you
in which case it's a good idea to save it via the "equation
notation" you mentioned.)
in the second example you *are* creating an object, and that
is why you would need to save it... if you didn't, you would
not have any way to access that object or the goodies inside.
hope this helps!!
cyberweb.consulting :: silicon.valley, ca