Python 3.2: Information on class, class attributes, and values of objects -


i'm new python, , i'm learning class function. know of sources/examples this? here's example wrote up. i'm trying read more self , init

class example:     def __init__(a, b, c, d):         self.a =         self.b = b         self.c = c         self.d = d  test = example(1, 1, 1, 1) 

i've been python.org, site. i've been reading beginners python books, i'd more information.

thanks.

a couple of generic clarifications here:

  • python's "class" keyword not function, it's statement signals language following code describes class (a user-defined data type , associated behavior). "class" takes name (and possibly empty list of "parent" classes) ... , introduces "suite" (an indented block of code).
  • the "def" keyboard similar statement defines function. in example, should have read: *def _init_(self, a, b, c)*) you're defining special type of function "part of" (associated with, bound to) example class. it's possible (and common) create unbound functions. commonly, in python, unbound functions simple called "functions" while part of class called "methods" ... or "instance functions."
  • classes templates instantiating objects. terms "instance" , "object" synonymous in context. example "test" instance ... , python interpreter "instantiates" , initializes object according class description.
  • a class "type", it's user definition of type of data , associated methods. "class" , "type" synonymous in python though conventionally used in different ways. core "types" of python data (integers, real numbers, imaginary/complex numbers, strings, lists, tuples, , dictionaries) referred "types" while more complex data/operational structures called classes. versions of python implemented constraints made distinction between "type" , "class" more merely matter of terminological difference. however, last several versions of python have eliminated underlying technical distinctions. distinctions related "subclassing" (inheritance).
  • classes can described set of additions , modifications class. called "inheritance" , class derived in manner referred "subclass." it's common programmers create hierarchies of classes ... specific variations deriving more common bases. it's common define related functionality within same files or sets of files. these "class libraries" , built "packages."
  • _init_() method; in particular it's initializer python objects (instances of class).
  • python uses _..._ (prefixing , suffixing pairs of underscore characters around selected keywords) "special" method or attribute names ... intended reduce likelihood naming choices conflict meaningful names might wish give own methods. while can name other methods , attributes _xxxx_ --- python not inherently treat error --- it's extremely bad idea so. if don't pick of defined special names there's no guarantee future version of python won't conflict usage later.
  • "methods" functions ... type of function bound (associated with) particular instance of particular class. there "class methods" associated class rather specific instance of class.
  • in example self.b, self.c , on "attributes" or "members" of object (instance). (the terms synonymous).
  • in general purpose of object orient programming provide ways of describing types of data , operations on types of data in way that's amenable both human comprehension , computerized interpretation , execution. (all programming languages intended strike balance between human readability/comprehension , machine parsing/execution; object-oriented languages focus on description , definition of "types," , instantiation of types "objects" , various interactions among objects.
  • "self" python specific convention naming special (and required) first argument bound method. it's "self" reference (a way code in method refer object/instance's own attributes , other methods without ambiguity). while can call first argument bound methods "a" (as you've unwittingly done in example) it's extremely bad idea so. not confuse later ... make no sense else trying read python code).
  • the term "object-oriented" confusing unless 1 aware of comparisons other forms of programming language. it's evolution "procedural" programming. simplest gist of comparison when consider sorts of functions 1 define in procedural language 1 might have define , separately name different functions perform analogous operations on different types of data: print_student_record(this_student) vs. print_teacher_report(some_report) --- programming model necessitates fair amount of overhead on part of programmer, keep track of functions work on types. sort of problem eliminated in oo (object oriented) programming 1 can, conceivably, call on this.print_() ... and, assuming 1 has created compatible classes, "print" regardless of whether "this" student (record/object) or teacher (report/record/object). that's oversimplified useful understanding pressures led development , adoption of oo based programming.
  • in python it's possible create classes little or no functionality. example nothing, yet, transfer set of arguments "attributes" (members) during initialization (instantiation). after use these attributes in programming statements like: test.a += 1 , print (test.a). possible because python "multi-paradigm" language. supports procedural object-orient programming styles. objects used way similar "structs" c programming language (predecessor c++) , "records" in pascal, etc. style of programming largely considered obsolete (particularly when using modern, "very high level" language such python).

the gist of i'm getting @ ... you'll want learn how think of data combination of it's "parts" (attributes) , functionality changes, manipulates, validates, , handles input, output, , possibly storage, of attributes.

for example if writing "code breaker" program solve ciphers might implement "histogram" object counts letter frequencies of given coded message. have attributes (one integer every letter) , behavior (feeding ports of coded message(s) instance, splitting strings individual characters, filtering out non-letter characters, converting letters upper or lower case, , counting them --- incrementing integer corresponding each letter). additionally you'd need have way of querying histogram ... example getting list of letters sorted frequency in cipher text.

once had such "histogram" class think of ways use solver. example solve cryptogram puzzle might computer histogram try substituting "etaon" 5 common ciphered letters ... check how many of "partial" strings (t.e "the") match words, trying permutations, , on. each of these might it's own class. key point of programming histogram class might useful counting sorts of other things (even in simple voting system or popularity context). particular subclass or instantiation might make histogram of letters while others re-used other types of "things" want counted. code iterates on permutions of list might used in number of simulation, optimization, , related programs. (in fact python's standard libraries including "counters" , "permutations" functions in "collections" , "itertools" modules, respectively).

of course you're going hear of of these concepts repeatedly study programming. has been rather rambling attempt kickstart process. know i've been bit repetitious in few points here --- part of because i'm typing 4am after having started work @ 7am yesterday; part of serves pedagogical purpose well.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -