class MyFirstClass:
pass
class MyFirstClass:
pass
a = MyFirstClass()
b = MyFirstClass()
print(a)
print(b)
<__main__.MyFirstClass object at 0x7a4548d5ead0> <__main__.MyFirstClass object at 0x7a453be4be80>
class Point:
pass
p1 = Point()
p2 = Point()
p1.x = 5
p1.y = 4
p2.x = 3
p2.y = 6
print(p1.x, p1.y)
print(p2.x, p2.y)
5 4 3 6
class Point:
def reset(self):
self.x = 0
self.y = 0
p = Point()
p.reset()
print(p.x, p.y)
0 0
import math
class Point:
def move(self, x, y):
self.x = x
self.y = y
def reset(self):
self.move(0, 0)
def calculate_distance(self, other_point):
return math.sqrt(
(self.x - other_point.x)**2 +
(self.y - other_point.y)**2)
# how to use it:
point1 = Point()
point2 = Point()
point1.reset()
point2.move(5, 0)
print(point2.calculate_distance(point1)) # Debugging and Testing the code
assert (point2.calculate_distance(point1) ==
point1.calculate_distance(point2)) # assert permite realizar comprobaciones
point1.move(3, 4)
print(point1.calculate_distance(point2))
print(point1.calculate_distance(point1))
5.0 4.47213595499958 0.0
class Point:
def __init__(self, x, y):
self.move(x, y)
def move(self, x, y):
self.x = x
self.y = y
def reset(self):
self.move(0, 0)
# Constructing a Point
point = Point(3, 5)
print(point.x, point.y)
3 5
class Point:
def __init__(self, x=0, y=0):
self.move(x, y)
import math
class Point:
'Represents a point in two-dimensional geometric coordinates'
def __init__(self, x=0, y=0):
'''Initialize the position of a new point. The x and y coordinates can
be specified. If they are not, the point defaults to the origin.'''
self.move(x, y)
def move(self, x, y):
"Move the point to a new location in two-dimensional space."
self.x = x
self.y = y
def reset(self):
'Reset the point back to the geometric origin: 0, 0'
self.move(0, 0)
def calculate_distance(self, other_point):
"""Calculate the distance from this point to a second point passed
as a parameter.
This function uses the Pythagorean Theorem to calculate the distance
between the two points. The distance is returned as a float."""
return math.sqrt(
(self.x - other_point.x)**2 +
(self.y - other_point.y)**2)
class Database:
# the database implementation
pass
database = Database()
class Database:
# the database implementation
pass
database = None
def initialize_database():
global database
database = Database()
class UsefulClass:
'''This class might be useful to other modules.'''
pass
def main():
'''creates a useful class and does something with it for our module.'''
useful = UsefulClass()
print(useful)
if __name__ == "__main__":
main()
<__main__.UsefulClass object at 0x7a453be921a0>
def format_string(string, formatter=None):
'''Format a string using the formatter object, which
is expected to have a format() method that accepts
a string.'''
class DefaultFormatter:
'''Format a string in title case.'''
def format(self, string):
return str(string).title()
if not formatter:
formatter = DefaultFormatter()
return formatter.format(string)
hello_string = "hello world, how are you today?"
print(" input: " + hello_string)
print("output: " + format_string(hello_string))
input: hello world, how are you today? output: Hello World, How Are You Today?
class SecretString:
'''A not-at-all secure way to store a secret string.'''
def __init__(self, plain_string, pass_phrase):
self.__plain_string = plain_string
self.__pass_phrase = pass_phrase
def decrypt(self, pass_phrase):
'''Only show the string if the pass_phrase is correct.'''
if pass_phrase == self.__pass_phrase:
return self.__plain_string
else:
return ''
s = SecretString("Los pilares de la tierra", "abcde")
s.decrypt("abcde")
'Los pilares de la tierra'
import datetime
last_id = 0
class Note:
'''Represent a note in the notebook. Match against a
string in searches and store tags for each note.'''
def __init__(self, memo, tags=''):
'''initialize a note with memo and optional
space-separated tags. Automatically set the note's
creation date and a unique id'''
self.memo = memo
self.tags = tags
self.creation_date = datetime.date.today()
global last_id
last_id += 1
self.id = last_id
def match(self, filter):
'''Determine if this note matches the filter
text. Return True if it matches, False otherwise.
Search is case sensitive and matches both text and
tags.'''
return filter in self.memo or filter in self.tags
n1 = Note("Programación orientada a objetos en Python", "POO,Python,programación")
n1.match("Python")
True