0

I have the following classes in Python:

class String:    
    def clean_string(self, corpus):
        f = open(corpus, 'r')
        raw = f.read().lower()
        f.close()
        raw1 = re.sub(omissis, '', raw) 
        self.string = raw1

    def print_string(self):
        return self.string

class Set:
    def letters_set(self, string):
        self.let_set = set(re.findall(omissis, string))

class Dict:
    def __init__(self, dictionary={}):
        self.dictionary = {} 
        self.string = String()
        self.let_set = Set() 

    def generate_possible_triplets(self, let_set):
        triplet = [(ch1, ch2, ch3) for ch1 in let_set
                                   for ch2 in let_set
                                   for ch3 in let_set]
        [...]

I have a problem with objects as function arguments. Suppose I want to create an instance of the class Set, one of class String and call the method .letters_set(String.string).

What do I have to put as argument inside the parenthesis? the name of the object of class string I will create? a variable referenced to this object? (same applies for the method .generate_possible_triplets in Dict. What form should let_set take?

8
  • the variable that references to the object you want to pass Commented Oct 10, 2012 at 20:28
  • Your letters_set() just expects a normal python string. Is that your intention or are you trying to integrate it with your custom String class? Commented Oct 10, 2012 at 20:32
  • any variable references to an object? just make one and pass it like in foo = 0; func(foo) Commented Oct 10, 2012 at 20:34
  • 1
    On a scale of 1 to "don't ever do that", naming classes after built-in data types is pretty bad. You have UserDict and UserString if you want to extend them - allowing you to add methods while still keeping the basic functionality of the underlying type. Commented Oct 10, 2012 at 20:58
  • 1
    @SeanMcSomething: I agree with the first part about the naming for sure. The UserDict, UserString, etc are kind of old (python 2.2) when there was still old-style classes: The need for this class has been largely supplanted by the ability to subclass directly from dict (a feature that became available starting with Python version 2.2). Commented Oct 10, 2012 at 21:08

1 Answer 1

1

You would probably just want to make your methods accept instances of your custom classes...

class String:
    ...

class Set:
    def letters_set(self, stringObj):
        # stringObj is a String instance
        self.let_set = set(re.findall(omissis, stringObj.string))

class Dict:
    ...

    def generate_possible_triplets(self, setObj):
        # setObj is a Set instance
        triplet = [(ch1, ch2, ch3) for ch1 in setObj.let_set
                                   for ch2 in setObj.let_set
                                   for ch3 in setObj.let_set]


aString = String()
aSet = Set()
aDict = Dict()

aSet.letters_set(aString)
aDict.generate_possible_triplets(aSet)

The methods can then expect to operate on those classes appropriately to access the attributes. This example is not specifically checking the capabilities of the objects being passed in, but they would obviously raise an exception when you try to access an improper object type that does not have a .string or .let_set attribute.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.