0

How do I adapt, the checkLength() method, to accept any instance attributes I pass to it? For example in this context, I can check either first or last, without hardcoding it inside the checkLength() method?

Any help would be most appreciated.

class Name:
  def __init__(self, f, l):
    self.first = f
    self.last = l

  def checkLength(self):
    if type(self.first) == str:
      return len(self.first)
    else:
      return False

myName = Name('Sir', 'Mixalot') 
print(myName.checkLength())

I have tried the following combinations:

  def checkLength(self, last):
    if type(self.last) == str:
      return len(self.last)
    else:
      return False

print(myName.checkLength(last))
  def checkLength(self.last):
    if type(self.last) == str:
      return len(self.last)
    else:
      return False

print(myBody.checkLength(self.last))

Update - trying to use getattr

  def __init__(self, f, l):
    self.first = f
    self.last = l

  def checkLength(self, attr):
    if type(getattr(self, attr)) == str:
      return len(getattr(self, attr))
    else:
      return False

myName = Name('Sir', 'Mixalot') 
print(myName.checkLength(self, "last"))

error =

NameError: name 'self' is not defined````
6
  • Could you elaborate on exactly what is that you want to achieve with the checkLength function ? As in what is the desired use of this function ? Commented Dec 14, 2020 at 13:27
  • 1
    Why don't you simply use the len function directly? For example: print(len(myName.last)) Or, if you need a version of len which checks the type first, you can write this function (typeSafeLen) independently of your class. Commented Dec 14, 2020 at 13:30
  • Your question is not clear. According to what are you going to choose between checking the len of f or l? Commented Dec 14, 2020 at 13:38
  • 1
    Side-note: For type-checking, you want either if isinstance(self.first, str): (allows subclasses) or if type(self.first) is str: (strict test for that exact class). Don't use == str; the str class is a singleton, and if you want strict checking, use is for identity testing, not == (for loose value equality testing). Commented Dec 14, 2020 at 13:38
  • 1
    It should be print(myName.checkLength("last")) Commented Dec 14, 2020 at 13:42

1 Answer 1

1

Consider using getattr(object, name[, default]). For example:

class Name:
  def __init__(self, f, l):
    self.first = f
    self.last = l

  def checkLength(self, attr):
    if type(getattr(self, attr)) == str:
      return len(getattr(self, attr))
    else:
      return False

myName = Name('Sir', 'Mixalot') 
print(myName.checkLength("first"))

3

But you should also add some error handling when that attribute is missing.

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

2 Comments

thank you for your kind response, I am not familiar with getattr, your suggestion is most helpful. I updated my code in the original post. I am still struggling with this solution as I get a error message print(myBody.checkLength(self, last)). NameError: name 'self' is not defined
@dimButTries I thinks you forgot to put all that in the class.

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.