Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Alan G
> class Shape(object): > _count = 0 > > @classmethod > def count(cls): > try: > cls._count += 1 > except AttributeError: > cls._count = 1 Ah, clever. This is where I thought I'd need an if/elif chain, adding a new clause for each subclass. i never thought of usin

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Kent Johnson
Alan G wrote: > So If I have a heirarchy of shapes and want a class method that > only operates on the shape class itself, not on all the > subclasses then I have to use staticmethod whereas if I want > the class method to act on shape and each of its sub classes > I must use a classmethod. The can

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Alan G
- Original Message - From: "Kent Johnson" <[EMAIL PROTECTED]> > No, a classmethod is passed the class that it is called on. > If you have an inheritance tree you don't know this with a staticmethod. Aha! Like the OP I was aware of the class/no class distinction but couldn't see how this

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Kent Johnson
Chuck Allison wrote: > Hello Chinook, > > So is the main motivation for class methods so that you can have the > class object available? It seems you can have that anyway in a static > method by just asking. No, a classmethod is passed the class that it is called on. If you have an inheritance t

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Alan G
> Sorry for the elementary question: I was wondering if someone could > explain the difference to me between class and static methods. Coming > from other languages, I'm used to static methods, but not "class > methods". Thanks. There probably is a deep and subtle difference in Python but to all

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chinook
On Tue, 21 Jun 2005 17:58:09 -0400, Chuck Allison wrote (in article <[EMAIL PROTECTED]>): > Hello Chinook, > > So is the main motivation for class methods so that you can have the > class object available? It seems you can have that anyway in a static > method by just asking. I'm sure there's a g

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chuck Allison
Hello Chinook, So is the main motivation for class methods so that you can have the class object available? It seems you can have that anyway in a static method by just asking. I'm sure there's a good reason for this, but I haven't yet gotten to the point of mastery where I can see a need for clas

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chinook
On Tue, 21 Jun 2005 16:52:09 -0400, Chuck Allison wrote (in article <[EMAIL PROTECTED]>): > Sorry for the elementary question: I was wondering if someone could > explain the difference to me between class and static methods. Coming > from other languages, I'm used to static methods, but not "class

Re: [Tutor] Class reference problem?

2005-06-03 Thread [EMAIL PROTECTED]
> Hmmm... you may want to modify the print statements slightly to make > it more clear which of the two print statements are being displayed. > As the code stands, it's not clear that 'condition' is ever set to true. /me slaps himself in the forehead. After modifying the print statements as s

Re: [Tutor] Class reference problem?

2005-06-03 Thread Kent Johnson
[EMAIL PROTECTED] wrote: > for item in function1(args): > object = class() > if (function2(item)): > if (condition): > object.variable = value > object.function() > print object # debug > print object #debug > > The above pseudo code (

Re: [Tutor] Class reference problem?

2005-06-03 Thread Danny Yoo
On Fri, 3 Jun 2005, [EMAIL PROTECTED] wrote: > for item in function1(args): > object = class() > if (function2(item)): > if (condition): > object.variable = value > object.function() > print object # debug > print object #debug > > T

Re: [Tutor] Class and Scope Question

2005-05-06 Thread Tim Johnson
* Karl Pflästerer <[EMAIL PROTECTED]> [050506 10:40]: >Karl > -- > Please do *not* send copies of replies to me. > I read the list My Thanks to both Karl and Rich for help me to understand this problem. I also appreciate the documentation reference. cheers tim -- Tim Johnson <[EMAIL

Re: [Tutor] Class and Scope Question

2005-05-06 Thread Karl Pflästerer
On 6 Mai 2005, [EMAIL PROTECTED] wrote: > > The following test script is kind of got me baffled: >#!/usr/local/bin/python > class Eval: > def __getitem__(self,key): > return eval(key) >##def test(): >## i = 100 >## b = ["My", "name", "is", "Tim"] >## test = "this is number %(str(i)

Re: [Tutor] Class and Scope Question

2005-05-06 Thread Rich Krauter
Tim Johnson wrote: > The following test script is kind of got me baffled: > #!/usr/local/bin/python > class Eval: > def __getitem__(self,key): > return eval(key) > ##def test(): > ## i = 100 > ## b = ["My", "name", "is", "Tim"] > ## test = "this is number %(str(i))s for a test %(' '.

Re: [Tutor] Class and methods?

2005-03-31 Thread Alan Gauld
> I am sorta starting to get it. So you could use __init__ to ask for a > file name to see if there is one in a folder or not if there is then > open that file and conitue where that file left off. If its not there > create a new file with that name, then start the program? Or do I have > that all

Re: [Tutor] Class and methods?

2005-03-30 Thread Max Noel
On Mar 31, 2005, at 00:44, Kevin wrote: I am sorta starting to get it. So you could use __init__ to ask for a file name to see if there is one in a folder or not if there is then open that file and conitue where that file left off. If its not there create a new file with that name, then start the p

Re: [Tutor] Class and methods?

2005-03-30 Thread Kevin
I am sorta starting to get it. So you could use __init__ to ask for a file name to see if there is one in a folder or not if there is then open that file and conitue where that file left off. If its not there create a new file with that name, then start the program? Or do I have that all wrong? Th

Re: [Tutor] Class and methods?

2005-03-30 Thread Alan Gauld
> In a class is every def called a method Strictly speaking only those that have a 'self' parameter(or equivalent) The others are "unbound functions" and pretty useless, usually being the result of programmer errors!... > and the def __init__(self) is called the constructor method? Usually. >

Re: [Tutor] Class and methods?

2005-03-30 Thread Sean Perry
Kevin wrote: In a class is every def called a method and the def __init__(self) is called the constructor method? This class stuff is a little confusing. I don't have a problem writting a def I am still not clear on how to use a constructor. Is there a site that explains the constructor in great de

Re: [Tutor] Class in a class

2005-02-18 Thread Kent Johnson
Liam Clarke wrote: Hi Kent, So the layering is GUI - user interaction Application functionality CbDao - application-specific database access DbAccess - generic database access, easy to use JDBC connection - raw database access, not so easy to use This sounds a lot like what I'm aiming for in a

Re: [Tutor] Class in a class

2005-02-17 Thread Liam Clarke
Hi Kent, >So the layering is >GUI - user interaction >Application functionality >CbDao - application-specific database access >DbAccess - generic database access, easy to use >JDBC connection - raw database access, not so easy to use This sounds a lot like what I'm aiming for in a project, the

Re: [Tutor] Class in a class

2005-02-17 Thread Alan Gauld
> Does it make sense to do this: That depends on what you are trying to do! If its to make scrambled eggs thewn nope, no sense whatsoever, but if writing a programme storing an instance inside another instance is very common indeed! :-) > In [2]: class AB: >...: pass >...: > In [3

Re: [Tutor] Class in a class

2005-02-17 Thread Kent Johnson
Luis N wrote: Does it make sense to do this: In [2]: class AB: ...: pass ...: In [3]: a = AB() In [4]: a Out[4]: <__main__.AB instance at 0x8428bec> In [5]: class BC: ...: def __init__(self, foo): ...: self.foo = foo In [6]: b = BC(a) In [7]: b.foo Out[7]: <__main__.AB i

Re: [Tutor] Class within class, or...?

2005-01-26 Thread Alan Gauld
class Reader: def __init__(self, filePath=""): self.dataA=SchemaA() self.dataB=SchemaB() ... class SchemaA(): class SchemaB(): You probaly should put the Schema definitions before the Reader definition. Otherwise what you suggest is absolutely the norm for O

RE: [Tutor] class instance with identity crisis

2005-01-12 Thread Barnaby Scott
rning lots! Thanks > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Behalf Of Alan Gauld > Sent: 12 January 2005 20:13 > To: Alan Gauld; Barnaby Scott; 'Tutor' > Subject: Re: [Tutor] class instance with identity crisis > &

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Alan Gauld
Whoops, I forgot to do an assignment... > So try > > def dry(self): return Prune() > > > class Prune: > > def __str__(self): > > return 'prune' > > > > weapon = Damson() > > weapon.dry() weapon = weapon.dry() > > print weapon > > Should work as expected... Alan G __

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Alan Gauld
> My opinion is : this is a very dangerous and "stupid" thing to do !!! No its quite common. Its why C++ for example allows you to write your own type convcersion functions! One area where I've used this is to convert faults to orders in a service management application. Its fairly common for a c

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Alan Gauld
> class Damson: > def __str__(self): > return 'damson' > > def dry(self): > self = Prune() You'll neeed to return it for the outside world to use it... So try def dry(self): return Prune() > class Prune: > def __str__(self): > return 'prune' > > weapon

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Danny Yoo
On Wed, 12 Jan 2005, Barnaby Scott wrote: > I was wondering how you can get an instance of a class to change itself > into something else (given certain circumstances), but doing so from > within a method. So: > > class Damson: > def __str__(self): > return 'damson' > > def dry(s

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Jeff Shannon
Barnaby Scott wrote: class Damson: def __str__(self): return 'damson' def dry(self): self = Prune() class Prune: def __str__(self): return 'prune' weapon = Damson() weapon.dry() print weapon [...] but something in me suggests it should produce prune After all, 's

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Yigal Duppen
On Wednesday 12 January 2005 12:10, Kent Johnson wrote: > A couple of ideas: > > You could have dry() return the new weapon: >def dry(self): > return Prune() > > then the client code would be > weapon = weapon.dry() > > > You could have the weapon encapsulate another object and delegate to

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Kent Johnson
A couple of ideas: You could have dry() return the new weapon: def dry(self): return Prune() then the client code would be weapon = weapon.dry() You could have the weapon encapsulate another object and delegate to it. Finally, you actually can change the class of an object just by assigning t

Re: [Tutor] class instance with identity crisis

2005-01-12 Thread Pierre Barbier de Reuille
My opinion is : this is a very dangerous and "stupid" thing to do !!! Try to imagine the complexity of your program for someone who is trying to understand how your code is working if an object suddenly change its own type !!! Clearly, if you want to change the type of an object, you want a con

Re: [Tutor] class overriding question

2004-12-20 Thread Brian van den Broek
Alan Gauld said unto the world upon 2004-12-20 15:51: I hope that short history of OOP clarifies rather than confuses! :-) Alan G. Thanks for that Alan. Seems to clarify thing to me ;-) Best, Brian vdB ___ Tutor maillist - [EMAIL PROTECTED] http://mail

Re: [Tutor] class overriding question

2004-12-20 Thread Alan Gauld
> > Its another reason why you should never refer to > > an object or method *calling* another method > > (as Pilgrim does). Rather think of the method > > sending a *message* to the self object which > > invokes the appropriate method. > The contrast you suggest between calling a method and sendi

Re: [Tutor] class overriding question

2004-12-20 Thread Brian van den Broek
Alan Gauld said unto the world upon 2004-12-20 09:27: Its another reason why you should never refer to an object or method *calling* another method (as Pilgrim does). Rather think of the method sending a *message* to the self object which invokes the appropriate method. This decoupling of message

Re: [Tutor] class overriding question

2004-12-20 Thread Alan Gauld
You've got it right Brian. It is this ability to change the way *existing* code works that makes OOP more than a neat way to package data and functions. This is polymorphism at its most powerful. Its why in a C++/Java world the convention is to write hook methods that are called by the public meth

Re: [Tutor] class overriding question

2004-12-18 Thread Brian van den Broek
Kent Johnson said unto the world upon 2004-12-18 08:53: Yup, that's right! Attribute access (the dot operator '.') is an operation that happens at runtime, and each attribute access stands alone. Every attribute access goes through the same search path, starting with self, then the class (type)

Re: [Tutor] class overriding question

2004-12-18 Thread Kent Johnson
Yup, that's right! Attribute access (the dot operator '.') is an operation that happens at runtime, and each attribute access stands alone. Every attribute access goes through the same search path, starting with self, then the class (type) of self, finally the base classes. So, in your example, s

<    1   2   3   4