Python: calling class to print Tweet contents - python

I'm trying to filter tweets by hashtag and output the text of the Tweet.
The class Tweet contains the overarching tweet info, of which I only need "text". I know that str method needs to be used to call each instance of the class, but down below when I "print", the above class is inaccessible. Please help :D
Edit: what's the implication of replacing __str__ with __repr__ in class Tweet? Thanks!
class Tweet():
tweet = Tweet()
def __init__(self, tweet_dict = {}):
if "statuses" in tweet_dict:
self.status = tweet_dict["statuses"]
else:
self.status = ""
if "text" in self.status:
self.text = item["text"]
else:
self.text = ""
def __str__(self):
tweet_info = self.text
return tweet_info
#-------------------------------------------------------------------
tweet_inst = []
for dic in statuses:
item_instances = Tweet(dic)
tweet_inst.append(item_instances)
print(tweet_inst)

First of all, the second line, tweet = Tweet() in the class definition is wrong. You cannot create a class variable containing an instance of said class.
Second, you assign self.text = item["text"] in line 11. What is item? Should that rather be tweet_dict?
Third, you could directly return self.text in __str__, no need to assign it to a varibale tweet_info first.
Last, but not least, you should not use mutable datatypes as default arguments, like you do in line 4. Here's why.

__str__ needs to return a string representation of the object.
__str__ goal is to be readable, __repr__ goal is to be unambiguous.
When you print a list (which is tweet_inst), it iterates over the objects (of Tweet) contained in the list and call their __repr__.
You want to add def __repr__(self): return str(self) so they do the same thing, then it will return self.text instead of the object default representation (which is it's location in memory)
class Tweet(object):
def __init__(self, x):
self.x = x
def __str__(self):
return self.x
def __repr__(self):
return '__repr__' + str(self.x)
>>> t1 = Tweet('t1')
>>> t2 = Tweet('t2')
>>> list_ = [t1, t2]
>>> print(list_)
[__repr__t1, __repr__t2]
Rather than the default
[<__main__.Tweet object at 0x02AFA810>, <__main__.Tweet object at 0x029E2BF0>]
Here is a great answer by moshez about the difference between the two.

Related

How do I iterate over a list and print all attributes in a list [duplicate]

When I try to print an instance of a class, I get an output like this:
>>> class Test():
... def __init__(self):
... self.a = 'foo'
...
>>> print(Test())
<__main__.Test object at 0x7fc9a9e36d60>
How can I make it so that the print will show something custom (e.g. something that includes the a attribute value)? That is, how can I can define how the instances of the class will appear when printed (their string representation)?
See How can I choose a custom string representation for a class itself (not instances of the class)? if you want to define the behaviour for the class itself (in this case, so that print(Test) shows something custom, rather than <class __main__.Test> or similar). (In fact, the technique is essentially the same, but trickier to apply.)
>>> class Test:
... def __repr__(self):
... return "Test()"
... def __str__(self):
... return "member of Test"
...
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test
The __str__ method is what gets called happens when you print it, and the __repr__ method is what happens when you use the repr() function (or when you look at it with the interactive prompt).
If no __str__ method is given, Python will print the result of __repr__ instead. If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for printing.
As Chris Lutz explains, this is defined by the __repr__ method in your class.
From the documentation of repr():
For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.
Given the following class Test:
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"<Test a:{self.a} b:{self.b}>"
def __str__(self):
return f"From str method of Test: a is {self.a}, b is {self.b}"
..it will act the following way in the Python shell:
>>> t = Test(123, 456)
>>> t
<Test a:123 b:456>
>>> print(repr(t))
<Test a:123 b:456>
>>> print(t)
From str method of Test: a is 123, b is 456
>>> print(str(t))
From str method of Test: a is 123, b is 456
If no __str__ method is defined, print(t) (or print(str(t))) will use the result of __repr__ instead
If no __repr__ method is defined then the default is used, which is roughly equivalent to:
def __repr__(self):
cls = self.__class__
return f"<{cls.__module_}.{cls.__qualname__} object at {id(self)}>"
If you're in a situation like #Keith you could try:
print(a.__dict__)
It goes against what I would consider good style but if you're just trying to debug then it should do what you want.
A generic way that can be applied to any class without specific formatting could be done as follows:
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
And then,
elem = Element('my_name', 'some_symbol', 3)
print(elem)
produces
__main__.Element: {'symbol': 'some_symbol', 'name': 'my_name', 'number': 3}
A prettier version of response by #user394430
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return str(self.__class__) + '\n'+ '\n'.join(('{} = {}'.format(item, self.__dict__[item]) for item in self.__dict__))
elem = Element('my_name', 'some_symbol', 3)
print(elem)
Produces visually nice list of the names and values.
<class '__main__.Element'>
name = my_name
symbol = some_symbol
number = 3
An even fancier version (thanks Ruud) sorts the items:
def __str__(self):
return str(self.__class__) + '\n' + '\n'.join((str(item) + ' = ' + str(self.__dict__[item]) for item in sorted(self.__dict__)))
Simple. In the print, do:
print(foobar.__dict__)
as long as the constructor is
__init__
For Python 3:
If the specific format isn't important (e.g. for debugging) just inherit from the Printable class below. No need to write code for every object.
Inspired by this answer
class Printable:
def __repr__(self):
from pprint import pformat
return "<" + type(self).__name__ + "> " + pformat(vars(self), indent=4, width=1)
# Example Usage
class MyClass(Printable):
pass
my_obj = MyClass()
my_obj.msg = "Hello"
my_obj.number = "46"
print(my_obj)
Just to add my two cents to #dbr's answer, following is an example of how to implement this sentence from the official documentation he's cited:
"[...] to return a string that would yield an object with the same value when passed to eval(), [...]"
Given this class definition:
class Test(object):
def __init__(self, a, b):
self._a = a
self._b = b
def __str__(self):
return "An instance of class Test with state: a=%s b=%s" % (self._a, self._b)
def __repr__(self):
return 'Test("%s","%s")' % (self._a, self._b)
Now, is easy to serialize instance of Test class:
x = Test('hello', 'world')
print 'Human readable: ', str(x)
print 'Object representation: ', repr(x)
print
y = eval(repr(x))
print 'Human readable: ', str(y)
print 'Object representation: ', repr(y)
print
So, running last piece of code, we'll get:
Human readable: An instance of class Test with state: a=hello b=world
Object representation: Test("hello","world")
Human readable: An instance of class Test with state: a=hello b=world
Object representation: Test("hello","world")
But, as I said in my last comment: more info is just here!
You need to use __repr__. This is a standard function like __init__.
For example:
class Foobar():
"""This will create Foobar type object."""
def __init__(self):
print "Foobar object is created."
def __repr__(self):
return "Type what do you want to see here."
a = Foobar()
print a
__repr__ and __str__ are already mentioned in many answers. I just want to add that if you are too lazy to add these magic functions to your class, you can use objprint. A simple decorator #add_objprint will help you add the __str__ method to your class and you can use print for the instance. Of course if you like, you can also use objprint function from the library to print any arbitrary objects in human readable format.
from objprint import add_objprint
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
#add_objprint
class Player:
def __init__(self):
self.name = "Alice"
self.age = 18
self.items = ["axe", "armor"]
self.coins = {"gold": 1, "silver": 33, "bronze": 57}
self.position = Position(3, 5)
print(Player())
The output is like
<Player
.name = 'Alice',
.age = 18,
.items = ['axe', 'armor'],
.coins = {'gold': 1, 'silver': 33, 'bronze': 57},
.position = <Position
.x = 3,
.y = 5
>
>
There are already a lot of answers in this thread but none of them particularly helped me, I had to work it out myself, so I hope this one is a little more informative.
You just have to make sure you have parentheses at the end of your class, e.g:
print(class())
Here's an example of code from a project I was working on:
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return "{}: {}\nAtomic Number: {}\n".format(self.name, self.symbol, self.number
class Hydrogen(Element):
def __init__(self):
super().__init__(name = "Hydrogen", symbol = "H", number = "1")
To print my Hydrogen class, I used the following:
print(Hydrogen())
Please note, this will not work without the parentheses at the end of Hydrogen. They are necessary.
Hope this helps, let me know if you have anymore questions.
Even though this is an older post, there is also a very convenient method introduced in dataclasses (as of Python 3.7). Besides other special functions such as __eq__ and __hash__, it provides a __repr__ function for class attributes. You example would then be:
from dataclasses import dataclass, field
#dataclass
class Test:
a: str = field(default="foo")
b: str = field(default="bar")
t = Test()
print(t)
# prints Test(a='foo', b='bar')
If you want to hide a certain attribute from being outputted, you can set the field decorator parameter repr to False:
#dataclass
class Test:
a: str = field(default="foo")
b: str = field(default="bar", repr=False)
t = Test()
print(t)
# prints Test(a='foo')

Can't find how to print from list [duplicate]

When I try to print an instance of a class, I get an output like this:
>>> class Test():
... def __init__(self):
... self.a = 'foo'
...
>>> print(Test())
<__main__.Test object at 0x7fc9a9e36d60>
How can I make it so that the print will show something custom (e.g. something that includes the a attribute value)? That is, how can I can define how the instances of the class will appear when printed (their string representation)?
See How can I choose a custom string representation for a class itself (not instances of the class)? if you want to define the behaviour for the class itself (in this case, so that print(Test) shows something custom, rather than <class __main__.Test> or similar). (In fact, the technique is essentially the same, but trickier to apply.)
>>> class Test:
... def __repr__(self):
... return "Test()"
... def __str__(self):
... return "member of Test"
...
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test
The __str__ method is what gets called happens when you print it, and the __repr__ method is what happens when you use the repr() function (or when you look at it with the interactive prompt).
If no __str__ method is given, Python will print the result of __repr__ instead. If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for printing.
As Chris Lutz explains, this is defined by the __repr__ method in your class.
From the documentation of repr():
For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.
Given the following class Test:
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"<Test a:{self.a} b:{self.b}>"
def __str__(self):
return f"From str method of Test: a is {self.a}, b is {self.b}"
..it will act the following way in the Python shell:
>>> t = Test(123, 456)
>>> t
<Test a:123 b:456>
>>> print(repr(t))
<Test a:123 b:456>
>>> print(t)
From str method of Test: a is 123, b is 456
>>> print(str(t))
From str method of Test: a is 123, b is 456
If no __str__ method is defined, print(t) (or print(str(t))) will use the result of __repr__ instead
If no __repr__ method is defined then the default is used, which is roughly equivalent to:
def __repr__(self):
cls = self.__class__
return f"<{cls.__module_}.{cls.__qualname__} object at {id(self)}>"
If you're in a situation like #Keith you could try:
print(a.__dict__)
It goes against what I would consider good style but if you're just trying to debug then it should do what you want.
A generic way that can be applied to any class without specific formatting could be done as follows:
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
And then,
elem = Element('my_name', 'some_symbol', 3)
print(elem)
produces
__main__.Element: {'symbol': 'some_symbol', 'name': 'my_name', 'number': 3}
A prettier version of response by #user394430
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return str(self.__class__) + '\n'+ '\n'.join(('{} = {}'.format(item, self.__dict__[item]) for item in self.__dict__))
elem = Element('my_name', 'some_symbol', 3)
print(elem)
Produces visually nice list of the names and values.
<class '__main__.Element'>
name = my_name
symbol = some_symbol
number = 3
An even fancier version (thanks Ruud) sorts the items:
def __str__(self):
return str(self.__class__) + '\n' + '\n'.join((str(item) + ' = ' + str(self.__dict__[item]) for item in sorted(self.__dict__)))
Simple. In the print, do:
print(foobar.__dict__)
as long as the constructor is
__init__
For Python 3:
If the specific format isn't important (e.g. for debugging) just inherit from the Printable class below. No need to write code for every object.
Inspired by this answer
class Printable:
def __repr__(self):
from pprint import pformat
return "<" + type(self).__name__ + "> " + pformat(vars(self), indent=4, width=1)
# Example Usage
class MyClass(Printable):
pass
my_obj = MyClass()
my_obj.msg = "Hello"
my_obj.number = "46"
print(my_obj)
Just to add my two cents to #dbr's answer, following is an example of how to implement this sentence from the official documentation he's cited:
"[...] to return a string that would yield an object with the same value when passed to eval(), [...]"
Given this class definition:
class Test(object):
def __init__(self, a, b):
self._a = a
self._b = b
def __str__(self):
return "An instance of class Test with state: a=%s b=%s" % (self._a, self._b)
def __repr__(self):
return 'Test("%s","%s")' % (self._a, self._b)
Now, is easy to serialize instance of Test class:
x = Test('hello', 'world')
print 'Human readable: ', str(x)
print 'Object representation: ', repr(x)
print
y = eval(repr(x))
print 'Human readable: ', str(y)
print 'Object representation: ', repr(y)
print
So, running last piece of code, we'll get:
Human readable: An instance of class Test with state: a=hello b=world
Object representation: Test("hello","world")
Human readable: An instance of class Test with state: a=hello b=world
Object representation: Test("hello","world")
But, as I said in my last comment: more info is just here!
You need to use __repr__. This is a standard function like __init__.
For example:
class Foobar():
"""This will create Foobar type object."""
def __init__(self):
print "Foobar object is created."
def __repr__(self):
return "Type what do you want to see here."
a = Foobar()
print a
__repr__ and __str__ are already mentioned in many answers. I just want to add that if you are too lazy to add these magic functions to your class, you can use objprint. A simple decorator #add_objprint will help you add the __str__ method to your class and you can use print for the instance. Of course if you like, you can also use objprint function from the library to print any arbitrary objects in human readable format.
from objprint import add_objprint
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
#add_objprint
class Player:
def __init__(self):
self.name = "Alice"
self.age = 18
self.items = ["axe", "armor"]
self.coins = {"gold": 1, "silver": 33, "bronze": 57}
self.position = Position(3, 5)
print(Player())
The output is like
<Player
.name = 'Alice',
.age = 18,
.items = ['axe', 'armor'],
.coins = {'gold': 1, 'silver': 33, 'bronze': 57},
.position = <Position
.x = 3,
.y = 5
>
>
There are already a lot of answers in this thread but none of them particularly helped me, I had to work it out myself, so I hope this one is a little more informative.
You just have to make sure you have parentheses at the end of your class, e.g:
print(class())
Here's an example of code from a project I was working on:
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return "{}: {}\nAtomic Number: {}\n".format(self.name, self.symbol, self.number
class Hydrogen(Element):
def __init__(self):
super().__init__(name = "Hydrogen", symbol = "H", number = "1")
To print my Hydrogen class, I used the following:
print(Hydrogen())
Please note, this will not work without the parentheses at the end of Hydrogen. They are necessary.
Hope this helps, let me know if you have anymore questions.
Even though this is an older post, there is also a very convenient method introduced in dataclasses (as of Python 3.7). Besides other special functions such as __eq__ and __hash__, it provides a __repr__ function for class attributes. You example would then be:
from dataclasses import dataclass, field
#dataclass
class Test:
a: str = field(default="foo")
b: str = field(default="bar")
t = Test()
print(t)
# prints Test(a='foo', b='bar')
If you want to hide a certain attribute from being outputted, you can set the field decorator parameter repr to False:
#dataclass
class Test:
a: str = field(default="foo")
b: str = field(default="bar", repr=False)
t = Test()
print(t)
# prints Test(a='foo')

How do I find which row column is failing to be mapped? [duplicate]

When I try to print an instance of a class, I get an output like this:
>>> class Test():
... def __init__(self):
... self.a = 'foo'
...
>>> print(Test())
<__main__.Test object at 0x7fc9a9e36d60>
How can I make it so that the print will show something custom (e.g. something that includes the a attribute value)? That is, how can I can define how the instances of the class will appear when printed (their string representation)?
See How can I choose a custom string representation for a class itself (not instances of the class)? if you want to define the behaviour for the class itself (in this case, so that print(Test) shows something custom, rather than <class __main__.Test> or similar). (In fact, the technique is essentially the same, but trickier to apply.)
>>> class Test:
... def __repr__(self):
... return "Test()"
... def __str__(self):
... return "member of Test"
...
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test
The __str__ method is what gets called happens when you print it, and the __repr__ method is what happens when you use the repr() function (or when you look at it with the interactive prompt).
If no __str__ method is given, Python will print the result of __repr__ instead. If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for printing.
As Chris Lutz explains, this is defined by the __repr__ method in your class.
From the documentation of repr():
For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.
Given the following class Test:
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"<Test a:{self.a} b:{self.b}>"
def __str__(self):
return f"From str method of Test: a is {self.a}, b is {self.b}"
..it will act the following way in the Python shell:
>>> t = Test(123, 456)
>>> t
<Test a:123 b:456>
>>> print(repr(t))
<Test a:123 b:456>
>>> print(t)
From str method of Test: a is 123, b is 456
>>> print(str(t))
From str method of Test: a is 123, b is 456
If no __str__ method is defined, print(t) (or print(str(t))) will use the result of __repr__ instead
If no __repr__ method is defined then the default is used, which is roughly equivalent to:
def __repr__(self):
cls = self.__class__
return f"<{cls.__module_}.{cls.__qualname__} object at {id(self)}>"
If you're in a situation like #Keith you could try:
print(a.__dict__)
It goes against what I would consider good style but if you're just trying to debug then it should do what you want.
A generic way that can be applied to any class without specific formatting could be done as follows:
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
And then,
elem = Element('my_name', 'some_symbol', 3)
print(elem)
produces
__main__.Element: {'symbol': 'some_symbol', 'name': 'my_name', 'number': 3}
A prettier version of response by #user394430
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return str(self.__class__) + '\n'+ '\n'.join(('{} = {}'.format(item, self.__dict__[item]) for item in self.__dict__))
elem = Element('my_name', 'some_symbol', 3)
print(elem)
Produces visually nice list of the names and values.
<class '__main__.Element'>
name = my_name
symbol = some_symbol
number = 3
An even fancier version (thanks Ruud) sorts the items:
def __str__(self):
return str(self.__class__) + '\n' + '\n'.join((str(item) + ' = ' + str(self.__dict__[item]) for item in sorted(self.__dict__)))
Simple. In the print, do:
print(foobar.__dict__)
as long as the constructor is
__init__
For Python 3:
If the specific format isn't important (e.g. for debugging) just inherit from the Printable class below. No need to write code for every object.
Inspired by this answer
class Printable:
def __repr__(self):
from pprint import pformat
return "<" + type(self).__name__ + "> " + pformat(vars(self), indent=4, width=1)
# Example Usage
class MyClass(Printable):
pass
my_obj = MyClass()
my_obj.msg = "Hello"
my_obj.number = "46"
print(my_obj)
Just to add my two cents to #dbr's answer, following is an example of how to implement this sentence from the official documentation he's cited:
"[...] to return a string that would yield an object with the same value when passed to eval(), [...]"
Given this class definition:
class Test(object):
def __init__(self, a, b):
self._a = a
self._b = b
def __str__(self):
return "An instance of class Test with state: a=%s b=%s" % (self._a, self._b)
def __repr__(self):
return 'Test("%s","%s")' % (self._a, self._b)
Now, is easy to serialize instance of Test class:
x = Test('hello', 'world')
print 'Human readable: ', str(x)
print 'Object representation: ', repr(x)
print
y = eval(repr(x))
print 'Human readable: ', str(y)
print 'Object representation: ', repr(y)
print
So, running last piece of code, we'll get:
Human readable: An instance of class Test with state: a=hello b=world
Object representation: Test("hello","world")
Human readable: An instance of class Test with state: a=hello b=world
Object representation: Test("hello","world")
But, as I said in my last comment: more info is just here!
You need to use __repr__. This is a standard function like __init__.
For example:
class Foobar():
"""This will create Foobar type object."""
def __init__(self):
print "Foobar object is created."
def __repr__(self):
return "Type what do you want to see here."
a = Foobar()
print a
__repr__ and __str__ are already mentioned in many answers. I just want to add that if you are too lazy to add these magic functions to your class, you can use objprint. A simple decorator #add_objprint will help you add the __str__ method to your class and you can use print for the instance. Of course if you like, you can also use objprint function from the library to print any arbitrary objects in human readable format.
from objprint import add_objprint
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
#add_objprint
class Player:
def __init__(self):
self.name = "Alice"
self.age = 18
self.items = ["axe", "armor"]
self.coins = {"gold": 1, "silver": 33, "bronze": 57}
self.position = Position(3, 5)
print(Player())
The output is like
<Player
.name = 'Alice',
.age = 18,
.items = ['axe', 'armor'],
.coins = {'gold': 1, 'silver': 33, 'bronze': 57},
.position = <Position
.x = 3,
.y = 5
>
>
There are already a lot of answers in this thread but none of them particularly helped me, I had to work it out myself, so I hope this one is a little more informative.
You just have to make sure you have parentheses at the end of your class, e.g:
print(class())
Here's an example of code from a project I was working on:
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
def __str__(self):
return "{}: {}\nAtomic Number: {}\n".format(self.name, self.symbol, self.number
class Hydrogen(Element):
def __init__(self):
super().__init__(name = "Hydrogen", symbol = "H", number = "1")
To print my Hydrogen class, I used the following:
print(Hydrogen())
Please note, this will not work without the parentheses at the end of Hydrogen. They are necessary.
Hope this helps, let me know if you have anymore questions.
Even though this is an older post, there is also a very convenient method introduced in dataclasses (as of Python 3.7). Besides other special functions such as __eq__ and __hash__, it provides a __repr__ function for class attributes. You example would then be:
from dataclasses import dataclass, field
#dataclass
class Test:
a: str = field(default="foo")
b: str = field(default="bar")
t = Test()
print(t)
# prints Test(a='foo', b='bar')
If you want to hide a certain attribute from being outputted, you can set the field decorator parameter repr to False:
#dataclass
class Test:
a: str = field(default="foo")
b: str = field(default="bar", repr=False)
t = Test()
print(t)
# prints Test(a='foo')

Use an object method with the Initializer (Same line)

I'm cleaning up a python object class, focusing mainly on how the object is created. The __init__ method creates a an empty dictionary that needs to be filled almost instantly. But this should NOT happen within the __init__, as the method used will vary widely. Here's an example:
class Storage:
def __init__(self):
self.data = {}
def fill_1(self):
self.data['solo'] = 'all alone'
def fill_2(self, buddy, bff):
self.data['buddy'] = buddy
self.data['bff'] = bff
def fill_3(self, that_guy, house):
self.data[that_guy] = house
Normally, I can just call one after the other like so:
box = Storage.Storage()
box.fill_1()
However, this can be overwhelming when I create many of these objects sequentially. My goal is to use the __init__ method with one of the fill methods on the same line. I've tried using the call below:
box = Storage.Storage().fill_1()
But this does not create the object and instead returns None. So I have two questions:
Is my code returning a None object because the line is calling an instance method?
And how can I create the Storage object and then call it's fill method within the same line?
This is not an idiom you tend to see that often in python (though it's quite prevalent in many other languages, especially javascript), but you could do this by returning self from the mutator functions. (It looks like you were missing the self argument to the instance methods as well). This means you could also chain mutator calls -- Storage().fill_1().fill_2()
class Storage(object):
def __init__(self):
super(Storage, self).__init__()
data = {}
def fill_1(self):
data['solo'] = 'all alone'
return self
def fill_2(self, buddy, bff):
data['buddy'] = buddy
data['bff'] = bff
return self
def fill_3(self, that_guy, house):
data[that_guy] = house
return self
box = Storage().fill_1()
Make alternate constructors:
class Storage(object):
def __init__(self):
self.data = {}
#staticmethod
def filled_1():
obj = Storage()
obj.data['solo'] = 'all alone'
return obj
#staticmethod
def filled_2(self, buddy, bff):
obj = Storage()
obj.data['buddy'] = buddy
obj.data['bff'] = bff
return obj
#staticmethod
def filled_3(self, that_guy, house):
obj = Storage()
obj.data[that_guy] = house
return obj
Then you don't need to worry about separate creation and initialization calls, or muddle command-query separation with call chaining:
obj1 = Storage.filled_1()
obj2 = Storage.filled_2('Jenny', 'Joe')
...

Python: is there a way to "secretly" put text into a string?

I have been programming for about 2 months so I am relatively new to Python and programming in general.
I have a class with a field that will contain strings. I am trying to find a way to denote this string so that I can call it, but I don't want it to print the denotation. Is there a way to have a string contain text that won't be displayed when it is printed? This is what I mean by "secretly." I know I could make a separate field for this, and there are more complex ways to get around this, but I am wondering if the simple, easy process I am describing is possible.
So my use for this would be along the lines of:
class A(object):
def __init__(self, message):
self.message = message
x = A('\\magical marker\\this is a string.')
y = A("\\other magical marker\\this is a different string")
something = x # I am going to have a variable that could be many different things
if something.message.startswith('\\magical marker\\'):
print something
else:
pass
but when I print something, I want it to come out like this:
this is my string
sans the magical marker. Is there a way to do this?
You print something, which happens to be an instance of A. So you can define what happens when you try to print an object by defining a magic method __str__:
In [1]: class A:
...: def __init__(self, message):
...: self.message = message
...:
...: def __str__(self):
...: return self.message.split('}', 1)[-1]
...:
In [2]: x = A('{marker}my message')
In [3]: print(x)
my message
Note that printing x.message will print the whole string:
In [4]: x.message
Out[4]: '{marker}my message'
Changing that would be quite confusing.
How about re-programming the dot operator using properties.
class A(str):
def __init__(self, message):
self.message = message
#property
def message(self):
return 'this is my string'
#message.setter
def message(self, value):
self._message = value
x = A('\\magical marker\\this is a string.')
y = A("\\other magical marker\\this is a different string")
something = x
print x.message
This will always print 'this is my string' when you try to access the message field.
If you inherit from str you can simply str.replace:
class A(str):
def __init__(self, message):
self.message = message
x = A('\\magical marker\\this is a string.')
y = A("\\other magical marker\\this is a different string")
something = x # I am going to have a variable that could be many different things
if something.message.startswith('\\magical marker\\'):
print something.replace('\\magical marker\\',"",1)
A dirty little hack would be to use strings vs unicode strings:
x = 'this is a string.'
y = u'this is a unicode string.'
if isinstance(something, str):
print 'str'
else:
print 'unicode'
That only gets you two different classes, though. Maybe that's all you need?
Other answers have clever ideas that reveal interesting parts of Python... but miss the mark on matching your code to your goals, in a minimal and idiomatic way.
It sounds like the 'marker' is extra state that you want to remember and have affect later decisions.
So best would be to make that explicit: change your A class to accept another (probably optional) initialization argument:
class A(object):
def __init__(self, message, marker=None):
self.message = message
self.marker = marker
Then you later check the marker value directly and explicitly, without any 'hiding' or 'unpacking':
x = A('this is a string', marker='magic1')
if x.marker == 'magic1':
print x.message
else:
pass
This better describes the multifaceted nature of your A objects – even if their main purpose is carrying a string message, they've got other significant state, and it's good to give that state a distinctive name and place. You don't have to 'hide' it inside the main message – just give it its own slot.
(You might even move the conditional-logic, that determines what/whether to print, into another descriptively-named method on the A class – making instances of A responsible for their own display-rules.)
You could use something like this.
class SecretText(str):
def __str__(self):
"""Return empty string."""
return ''
def __repr__(self):
"""Return empty string."""
return self.__str__()
class SomewhatSecretText(str):
def __init__(self, *text):
"""Initialize a SomewhatSecretText instance."""
self.items = list(text)
assert self.verify_types()
# We have to keep some trace. Or we will
# never be able to get the original value.
self.message = ''.join(self.items)
def __str__(self):
"""Return string with hidden text."""
vis = ""
for item in self.items:
vis += str(item)
return vis
def __repr__(self):
"""Return string with hidden text."""
return self.__str__()
def __dir__(self):
"""
This is so self.message and self.items aren't
visible when dir() is called on the instance.
"""
return dir(str)
def verify_types(self):
"""Confirm instance items are strings."""
for item in self.items:
if not isinstance(item, str):
return False
return True
Using SomewhatSecretText, you can implement hidden text. Like this:
>>> hidden = SecretText("test")
>>> partly_hidden = SomewhatSecretText("This is a ", hidden, "!")
>>> print(partly_hidden)
This is a !
>>> print(repr(partly_hidden))
This is a !
>>> print(str(partly_hidden))
This is a !
You can get the full text back using the message variable.
>>> print(partly_hidden)
This is a !
>>> print(partly_hidden.message)
This is a test!
A bonus is that you can submit any number of strings to tag together. So you could have multiple hidden pieces.

Categories