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

class bvo:
    
    C = 'Hello'

    def getC(self):
        return self.C

bvo.getC()

Recommended Answers

All 3 Replies

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)

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.