Hey guys I have a study guide that I need some assistance with if possible. it is basically definitions.

Classes={}

Assume these statements are working with classes and objects:

Attribute?property

Instance

Encapsulation

Also I need to tell what this code does. I am not good with classes at all please can you break down each line.

class Critter(object):
     def __init__(self:
          print "A new critter is born!"
     def talk(self):
          print "\nHi. I am an instance of class critter.


#main
crit1 = Critter()
crit2 = Critter()
crti1.talk()
crit2.talk()

Edited 3 Years Ago by Nick Evan: Fixed formatting

With the code you have there (which you need to put in code tags next time :) ), is a good example of classes.

class Critter(object):
	def __init__(self):    					#you were missing a close bracket
		print "A new critter is born!"
	def talk(self):
		print "\nHi. I am an instance of class critter."       		#you were missing close quotes

#global scope!!!
crit1 = Critter()
crit2 = Critter()
crit1.talk()      							#"crit" was written incorrectly
crit2.talk()

Basically, the first line of code declares a class named "Critter". The second line is the __init__ function. This is called when you initialize an instance of the class, which will come up in a minute. The next line will just print that sentence to the console. As this is in the __init__ function, it will be printed when you initialize an instance of the class.

The "talk" function is basically a function that you can call that is associated with the class, that can be used with every instance of the class. This will again simply print a sentence to the console.

Now onto the main parts. Obviously, these are in the global scope, not in a main function. The first line in that section creates an instance of the "Critter" class, which is the value of the variable "crit1". This is pretty much the same as the next line. The next line calls the talk function from the instance of "Critter" assigned to the variable "crit1". The last line is pretty much the same, but with "crit2" instead.

Edited 6 Years Ago by SgtMe: n/a

Not sure about the constructor thing. Basically, it's just a function that is called when you create an instance of the class. It doesn't really matter much deeper than that. Just use it to initialize all your variables/do other stuff that needs to be done when you create the class instance.
Can you please upvote my above post seeing as it helped you out :) Thanks.

Edited 6 Years Ago by SgtMe: n/a

This question has already been answered. Start a new discussion instead.