Ejemplo n.º 1
0
from shirt import Shirt

shirt_one = Shirt('red', 'L', 'long-sleeve', 25)
print(shirt_one.price)
shirt_one.price = 10
print(shirt_one.price)
shirt_one.price = 20
print(shirt_one.price)
shirt_one.color = 'yellow'
print(shirt_one.color)
shirt_one.size = 'M'
print(shirt_one.size)
shirt_one.style = 'short_sleeve'
print(shirt_one.style)
print(shirt_one.color)
print(shirt_one.price)


shirt_two.change_price(45) # price change using method
print(shirt_two.price)


# How to access and change attribute values in python class

#  Shirt class has method to change shirt price
# Values can be changed either by assigning value or by  using method


shirt_one.color='Green'
print(shirt_one.color)


shirt_one.size='L'
print(shirt_one.size)


shirt_one.price=43
print(shirt_one.price)


# * There are some drawbacks to accessing attributes directly versus writing a method for accessing and displaying attributes.
# * Programming languages like C++/C  you can explicitely state wheather or not an object should be allowed to change or access attribute      values directly.
# * Changing value via method gives more flexibility in long term
from shirt import Shirt  # import Shirt class from python file

shirt_one = Shirt('red', 'M', 'long sleeve', 35)
shirt_two = Shirt('red', 'S', 'short sleeve', 30)

print(shirt_one.price)
print(shirt_two.price)

shirt_two.price_change(45)

print(shirt_one.price)
print(shirt_two.price)

# alternatively, we can chage the attribute, like price using accessing attributes
# this approach has its own drawbacks
# so, it's recommended to change the attribute value with method
# since it brings us more flexibility, when, for example, our measure units change
# we can specify the change once inide class method and use this afterwards

shirt_one.color = 'yellow'
shirt_two.size = 'L'

print(shirt_one.color)
print(shirt_two.size)