Diego Casso
Diego Casso

Reputation: 11

How can I use a class variable in its defined functions?

So I have a class object and I want to define a global variable for it, so that the functions defined within this class can use it. This variable is not actually variable it's just a value that I'll use in the functions.

So far I've tried what's shown below and it works but I'd like the value to be exclusive for the class.

alpha = "abcdefghijklmnopqrstuvwxyz"
key = 30


class Caesar:
    def __init__(self, phrase):
        self.phrase = phrase.casefold()

    def encrypt(self):
        y2 = ""
        for ltr in self.phrase:
            if ltr in alpha:
                cntr = alpha.index(ltr)
                if (cntr + key) > 25:
                    module = (cntr + key) % (len(alpha))
                    if (cntr + module) > 25:
                        foo = cntr - module
                        y2 += alpha[cntr - foo]
                    else:
                        y2 += alpha[module]
                else:
                    y2 += alpha[cntr + key]
            else:
                y2 += " "
        return y2

What I'd like to do is to place the alpha variable inside the class, and well, the key variable I'd like it to be variable as well but with a default value, of course being available for the encrypt() function.

Upvotes: 0

Views: 77

Answers (2)

Paulo Rodrigo Martins
Paulo Rodrigo Martins

Reputation: 26

In my opinion, you do not need any class nor an alphabet for that. You can use a simple function with a default value key=30 and use the integer representation of characters, i.g ord('a')=97 and ord('z')=122. You can use the casefold inside the function, then you could see if the letters are in the interval of the alphabet by comparing them with 'a' and 'z'. Finally, the module works the same, but the indexes goes from 97-122 instead of 0-25.

def encrypt(phrase,key=30):
    encrypted = ""
    for ltr in phrase.casefold():
        if ltr > 'a' and ltr < 'z':
            module = (ord(ltr)+key) %26
            new_ltr = chr(ord('a')+module)
        else:
            new_ltr = " "
        encrypted +=new_ltr
    return encrypted
print(encrypt("Here''s a test"))
#Output:'ebob  p   qbpq'

Upvotes: 0

Green Cloak Guy
Green Cloak Guy

Reputation: 24711

You can just put those declarations inside the class, and then refer to them later by using the class as a namespace:

class Caesar:
    alpha = "abcdefghijklmnopqrstuvwxyz"
    key = 30

    def __init__(self, phrase):
        self.phrase = phrase.casefold()

    def encrypt(self):
        alpha = Caesar.alpha
        key = Caesar.key

        y2 = ""
        for ltr in self.phrase:
            if ltr in alpha:
                cntr = index(ltr)
                if (cntr + key) > 25:
                    module = (cntr + key) % (len(alpha))
                    if (cntr + module) > 25:
                        foo = cntr - module
                        y2 += alpha[cntr - foo]
                    else:
                        y2 += alpha[module]
                else:
                    y2 += alpha[cntr + key]
            else:
                y2 += " "
        return y2

You could also just keep your original code and replace each instance of alpha with Caesar.alpha (and do the same for key), but doing that just once at the beginning of the method is more clear, in my opinion.

If you wanted to implement polymorphism (that is, you wanted to make subclasses of Caesar that might use different values for alpha or key), then you might instead consider using self.__class__ to obtain the values of alpha and key, instead of using the name Caesar explicitly:

    def encrypt(self):
        alpha = self.__class__.alpha
        key = self.__class__.key
        ...

Upvotes: 1

Related Questions