[Tutor] is this code dangerous?

Aztech Guy aztech1200@yahoo.com
Wed Jan 8 11:23:59 2003


Hi,

[I'm fairly new to Python myself too, so take what I
say with a grain of salt and test it out.]

Answers inline in your message.

Az.

--- Alfred Milgrom <fredm@smartypantsco.com> wrote:
> Hi:
> 
> Can someone please tell me the best way to override
> class methods for 
> specific instances?

First, based on your code below, I think you may
actually mean 'instane method' instead of class
method. (Sorry if I'm wrong about that). [Not sure if
class methods are allowed in Python or not - not got
that far in the tut/docs
 yet :-)] Class methods are different from instance
methods. They're allowed in C++ and Java. A class
method is a method that is not associated with a
specific instance of that class; its declaration /
definition syntax is different than for instance
methods. Also, a class method can be called - and
typically is called - without having to instantiate
even one instance of the class, whereas an instance
method can only be called when you *have* an instance,
using the syntax : inst.meth(args)
 - whereas a class method is called by syntax
something like : class_name.meth(args) (don't remember
now, but I think it's class_name::meth(args) in C++.

So, assuming you mean 'instance method' :
Not sure if it can be done in Python. I do know that
it is not possible in C++ and Java but is possible in
Ruby. Maybe newer versions of Python allow it. Check
the docs - or wait for a reply from someone else :-)
> 
> I want to have a general class, such as Person, with
> general methods. Some 
> of the instances of the Person class may have
> different methods, and I want 
> to be able to override the base method (such as the
> speak method for the 
> cranky person in the example below).
> 
> Do I need to define a new class for each instance
> where there is a 
> different method? 

See my answers above for whether it is possible in the
first place. If it is possible, then the answer to
question just above is yes, since, if you don't do it,
all instances will have the exact same methods.

Is there overhead associated with
> having a large number 
> of classes?
Depends on what kind of overhead you mean.
You'll surely have more source code overhead - as in
quantity of code - when you have more classes.
Also, if you have more code, it will take longer to
load, and occupy more space in memory while the code
is running - I would think both of these would be,
roughly, directly proportional to the amount of code.
This may or may not be a concern to you, depending on
your hardware configuration - CPU, RAM, etc.

> 
> Is the following code dangerous? (Obviously I could
> change the name of the 
> cranky class to crankyperson, or something like
> that, and have a different 
> class for each instance). What is the best way to
> handle this?
No, your code below is not 'dangerous'. What makes you
think it might be ? Actually, for the need you've
described, its the right code - if you plan to have
categories - i.e. classes - of people, such as normal
people (class Person) and cranky people (class
cranky). If you can categorize your people instances
that you need to create into a fairly small number of
such classes, then you can create a separate class for
each, each differing somewhat in the methods that they
have. (You can in fact have different methods in each
class, not just different implementations of the same
method). However, if your code is going to have a
really large number of individual people instances,
and you want different behaviour for each of them,
then it's going to be a problem for you - you'll have
to type out the code for the different methods that
each one has. Then this becomes something like you're
attacking the problem with a wrong approach. You might
then want to reconsider your design - one possible way
could be to analyse your app, and try to identify a
smaller number of categories of people, define classes
for each (e.g. Normal, Cranky, Deaf-Mute, Anti-Social,
etc.) and then parameterize the constructors so as to
be able to pass different values for some argument,
and within the speak method, use the different values
to generate different behaviour, using a nested
if/elif/..../else statement.


HTH

Az

> 
> ************************
> 
> class Person:
>      def __init__(self, name):
>          self.name = name
> 
>      def speak(self):
>          print "%s says: my name is %s" %
> (self.name, self.name)
> 
> hobbit = Person('Bilbo Baggins')
> 
> class cranky(Person):
>      def speak(self):
>          print "%s says: I don't tell anyone my
> name" % (self.name)
> 
> cranky = cranky('someone else')
> 
> hobbit.speak()
> cranky.speak()
> 
> ************************
> 
> Thanks in advance,
> Fred Milgrom
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com