Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
86 lines (61 loc) · 1.36 KB

File metadata and controls

86 lines (61 loc) · 1.36 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

objects

Examples in Python 3

"""
Object class
"""
class Shape(object):
	x = 0
	y = 0

print(Shape) # class '__main__.Shape'

"""
Object instance
"""
circle = Shape()

print(circle) 					# __main__.Shape object at 0x7f9cdf2f3278

"""
Add attribute to main object
"""
setattr(Shape, "name", "newShape")

print(circle.name) 				# newShape

"""
Add attribute to instance
"""
circle.z = 123

print(circle.z) 				# 123

"""
Method to print an objects own attributes
"""
# inside object class
def print(self):
	print(self.name, self.x, self.y, self.z)

circle.print() 					# newShape 0 0 123

"""
Method to change an objects own attributes
"""
# inside object class
def changeName(self, newName):
	self.name = newName

circle.changeName("anotherName")
print(circle.name) 				# anotherName

"""
Inititalize an object with attributes
"""
class Dog:
	kind = "Poodle" 			# global attribute
	def __init__(self, name):
		self.name = name 		# private for each instance 

dogOne = Dog("Aramis")
dogTwo = Dog("Athos")

print(dogOne.kind)				# Poodle
print(dogOne.name)				# Aramis

print(dogTwo.kind)				# Poodle
print(dogTwo.name)				# Athos

Reference and read more

Not done

Revision history

2014-07-21 (sylvanas) PA1 First try.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.