Talk:Classes

From GeoMod

Jump to: navigation, search

How to creat a new class inheriated from a built-in class? For example, when I want statistic the total counts of a specific tile has been hitted. There're lots way to do it. The easy way I think, is to create a new class name tile, tile inheriate all the elements and methods form class box, but have new integer element, count. When the ball hit the tile, tile.count = tile.count + 1.

Jianchen


Q 2) What is difference between 'class' and declaring 'function'?


"class" is a abstract description of the data and method inside of it. Accordingly, an object is an instance of a class. The 'method' here, could be a procedure (no required argument, e.g., print().), or a 'function'(require some argument, e.g, a math function, sqrt(X).) So, the difference is you must declar a function inside a class. function is just one part of a class.

You need declare a class first (include some function declaration in it), and define an object based on the definition of your declared class; and then you can use the object to refer your declared function. I know it is hard to understand, if you haven't any programing experience before.

Jianchen


Q 3) How to declare a private variable for a class?

I found some information from "An Introduction to Python"(by Guido van Rossum and Fred L. Drake, Jr.), but still confuse with it. the following is the desciption on http://www.network-theory.co.uk/docs/pytut/tut_77.html:


11.6 Private Variables There is limited support for class-private identifiers. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is now textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard of the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, as well as globals, and even to store instance variables private to this class on instances of other classes. Truncation may occur when the mangled name would be longer than 255 characters. Outside classes, or when the class name consists of only underscores, no mangling occurs.

Name mangling is intended to give classes an easy way to define "private" instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger, and that's one reason why this loophole is not closed. (Buglet: derivation of a class with the same name as the base class makes use of private variables of the base class possible.)

Notice that code passed to exec, eval() or evalfile() does not consider the classname of the invoking class to be the current class; this is similar to the effect of the global statement, the effect of which is likewise restricted to code that is byte-compiled together. The same restriction applies to getattr(), setattr() and delattr(), as well as when referencing __dict__ directly.

Jianchen

Personal tools