Chapter 8 (Objects basics)

Key-terms

What is a class?

This is the 'blueprint' for an object

What is an object?

This is a set of methods and attributes

What is an attribute?

This is a variable of the object

What is a method?

This is a function of the object

What is does instantiate mean?

This means to create an instance from a class

What is an instance?

This is an individual object

Syntax for a Class

What is the syntax for a class?
class ObjectThing(object):
	#note that it is good practice to start class names with a capital letter
	#also notice that i am basing the class off of object
	methods
	attributes

How do i define a method?
class ClassName(object):
#defineing the class
	def methodName(self):
	#defining the method, you must have the parameter 'self'
		code
		code
How to instantiate an object?

objectName = ClassName()

How do i invoke a method?

objectName.methodName()

What does a constructor method do?

This gives all the attributes all there values

How do i create a constitutor or initialization method?
class ClassName(object):
	def __init__(self):
		#code that you want to run when you initalize the object
  • The __init__ is recognized by python as an initialization method, and is therefore run when the object is instantiated.
How would i initialize an attribute?
class ClassName(object):
	def __init__(self, theNameGiven, age):
		self.name = theNameGiven
		self.age = age

objectName = ClassName('ben', 19)
What does the __str__() method do?
class ClassName(object):
	def __init__(self):
		self.name = 'jim'
		self.age = '19'
		#defining the initalisation method
	
	def __str__(self):
		return 'my name is {}, and my age is{}'.format(self.name, self.age)
		#returns the string above when called

objectName = ClassName()
print(str(objectName))
#returns:
#my name is jim, and my age is 19
What is a class attribute?

This is a attribute that is the same for all objects in a class, when a class attribute is changed in one object they are changed across the entire class.

What is a static method?

This is a method that is the same for all objects in a class

How do i create a class attribute?
class ClassName(object):
	classAttributeName = 10

	def changingClassAttribute(self):
		ClassName.classAttributeName += 1
		#notice how i havent used the prefix self, i have used the class name

How do i create and invoke a static method?
class ClassName(object):
	
	@staticmethod
	def staticMethodName():
		#note how i didnt call for self, this is 
		#because it is not associated with a object
		code

ClassName.staticMethodName()
#note we are calling it through the class not an object

Public and Private attributes

What is a public attribute?

This is an attribute that can be accessed and manipulated outside of an objects scope

What is a private attribute?

This is an attribute that can only be accessed and manipulated within an objects scope, via methods

How do i create a private attribute?
class ClassName(object):
	def __init__(self):
		self.name = 'ben'
		#public attribute
		self.__surname = 'lj'
		#privet attribute

Note: it is still possible to access the private method

print(objectName._Classname__surname)

BUT THIS SHOULD NEVER BE DONE!!!

How to create a private method?
class ClassName(object):
	def __privateMethod(self):
		code

Decorators

What is a decorator?

It allows you to add additional functionality to a function

An example is that you can run some checks on your parameters to make sure that they would run with your function

How do i create a decorator and call it?
def check(functionYouWantToCheck):
	def theBitThatChecks(input1, input2):
		if checksForSomething:
			#does some check
			code
		else:
			functionYouWantToCheck(input1, input2)
			#under these conditions we want our funtion to run

	return theBitThatChecks
	#runs the function we have declared 


@check
functionThatWillBePassedIntoCheck(input1,input2)
#essentually encapualates the function, runs check

Decorator exsamples

What is a property?

This is a way to control how you show a client an attribute or how they change it

What does the property decorator do?

It allows you to call a method as if it was a attribute, which can means you can perform functionality to the output

What is the syntax for a property?
class ClassName(object):
	def __init__(self):
		self.__name = 'ben'
	
	@property
	#property decorator
	def accessToName(self):
		return self.__name + ' wow'

objectName = ClassName()
print(objectName.accessToName)
#prints:
#ben wow
What does the attributeName.setter do?

This allows you to call a setting method like it was an attribute

What is the syntax for the setter decorator?
class ClassName(object):
    def __init__(self):
        self.__name = 'ben'
    
    @property
    def name(self):
        return self.__name
    
    @name.setter
    def name(self, newName):
        self.__name = newName

obj = ClassName()
print(obj.name)
#prints: ben

obj.name = 'pedro'
print(obj.name)
#prints: pedro