0

I would like to use string interpolation within instance methods: but am unsure how to access instance variables.

Here is the basic class declaration including the constructor:

class Validation: def __init__(self, name, type, count, script): self.name = name self.type = type self.count = count self.script = script 

And here is where we want to access the list of variables. Note "globals()" is not correct..

 def __repr__(self): return "%(self.name)s: %(self.type)s that expects %(self.count)s Rows [%(self.script)s]" %self.__dict__ 

But that gives exception

(<type 'exceptions.KeyError'>, KeyError('self.name',), None) 

So the question is: what should be used in place of %self.dict here?

5
  • 4
    You can omit the self.: "%(name)s: %(type)s that expects %(count)s Rows [%(script)s]" % self.__dict__. Commented Mar 21, 2014 at 20:06
  • 1
    what @alecxe said. also vars(self) is a little nicer on the eyes than self.__dict__. Commented Mar 21, 2014 at 20:08
  • 1
    Why not just return "%s: %s that expects %s Rows [%s]" % (self.name, self.type, self.count, self.script)? It's as short as what you've already written and more refactor-friendly. Commented Mar 21, 2014 at 20:08
  • 1
    Avoid using __dict__ directly. If one of those is a property instead of an instance attribute (or if the class uses __slots__), the variable won't be in __dict__. This is also true of using vars, actually. It really is best to just explicitly pass in the variables you want to use. Commented Mar 21, 2014 at 20:09
  • @alecxe Pls make that an answer and I will accept it (with roippi's tweak as well) Commented Mar 22, 2014 at 0:33

2 Answers 2

3

First of all, string interpolation has been replaced by string formatting, so let's update that:

"{self.name}: {self.type} that expects {self.count} Rows [{self.script}]".format(**self.__dict__) 

Second of all, since you're giving it the dict to work with, the keys in the dict are just the variable names, not self.varname.

"{name}: {type} that expects {count} Rows [{script}]".format(**self.__dict__) 

Third of all, __repr__ is intended to be valid-ish Python code to reproduce the object. Maybe you meant to put this as __str__? The __repr__ should be

"Validation({name},{type},{count},{script})".format(**self.__dict__) 

or honestly, since that's bad practice, just:

"Validation({},{},{},{})".format(self.name,self.type,self.count,self.script) 

Let's try it:

#DEMO class Validation(object): def __init__(self, name, type_, count, script): self.name = name self.type = type_ self.count = count self.script = script def __repr__(self): return "Validation({},{},{},{})".format(self.name,self.type,self.count,self.script) def __str__(self): return "{}: {} that expects {} Rows [{}]".format(self.name,self.type,self.count,self.script) >>> v = Validation('MyName','integer',26,lambda x: x) >>> print(v) # or str(v) MyName: integer that expects 26 Rows [<function <lambda> at 0x02D75468>] >>> repr(v) 'Validation(MyName,integer,26,<function <lambda> at 0x02D75468>)' 
Sign up to request clarification or add additional context in comments.

Comments

1

You should omit the self. and use vars() instead of __dict__ (thanks to @roippi):

class Validation: def __init__(self, name, type, count, script): self.name = name self.type = type self.count = count self.script = script def __repr__(self): return "%(name)s: %(type)s that expects %(count)s Rows [%(script)s]" % vars(self) validation = Validation('name', 'type', 'count', 'script') print validation 

prints:

name: type that expects count Rows [script] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.