0

Please, please explain to me why this is giving me an error:

class bvo:
    
    C = 'Hello'

    def getC(self):
        return self.C

bvo.getC()
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by geekman89
0

Please, please explain to me why this is giving me an error:

class bvo:
    
    C = 'Hello'

    def getC(self):
        return self.C

bvo.getC()
# to ease debugging and understandir this should be Bvo (PEP8)
class bvo:
    # this is one per class class variable, not instance variable    
    # to ease debugging and understandir this should be message (descriptive and lower case)
    #(PEP8)
    C = 'Hello'
    # by PEP 8, this could be get_message(self)
    def getC(self):
        # this would then become return self.message
        return self.C

# this is not recommended
print bvo().getC()

print('\nSanitized\n')
class Bvo(object):
    def __init__(self, message = 'Hello'):
        self. message = message

# this is recommended, getters and setters are not needed
my_test = Bvo()
print(my_test.message)
my_test.message = 'Bye!'
print(my_test.message)

Edited by pyTony: n/a

0

thank you very much for not only solving why the code didn't work (forgot one () ), but for also showing me some good python programming practices. thank you :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.