Is everything in Python castable to a string? - python

I'm trying to find an example of something in Python that can't be cast to a string.
>>> str(None)
'None'
>>> str(False)
'False'
>>> str(5)
'5'
>>> str(object)
"<class 'object'>"
>>> class Test:
... pass
...
>>> str(Test)
"<class '__main__.Test'>"
>>> str(Test())
'<__main__.Test object at 0x7f7e88a13630>'
Is there anything the entire Python universe that cannot be cast to str?

From the __str__ documentation:
The default implementation defined by the built-in type object
calls object.__repr__().
and object.__repr__ prints object name and address (at least in cpython). That's where your output '<__main__.Test object at 0x7f7e88a13630>' comes from. A class would have to override __str__ and raise an exception (or have a bug) to fail. There is little reason to do this and you'd be hard-pressed to find one that wasn't built to purpose.

Is everything in Python castable to a string?
Nope!
>>> class MyObject():
... def __str__(self):
... raise NotImplementedError("You can't string me!")
...
>>> str(MyObject())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __str__
NotImplementedError: You can't string me!

Related

What is the type of Python's code object?

Is there a way I can compare to the type of a code object constructed by compile or __code__ to the actual code object type?
This works fine:
>>> code_obj = compile("print('foo')", '<string>', 'exec')
>>> code_obj
<code object <module> at 0x7fb038c1ab70, file "<string>", line 1>
>>> print(type(code_obj))
code
>>> def foo(): return None
>>> type(foo.__code__) == type(code_obj)
True
But I can't do this:
>>> type(foo.__code__) == code
NameError: name 'code' is not defined
but where do I import code from?
It doesn't seem to be from code.py. It's defined in the CPython C file but I couldn't find the Python interface type for it.
You're after CodeType which can be found in types.
>>> from types import CodeType
>>> def foo(): pass
...
>>> type(foo.__code__) == CodeType
True
Note that there's nothing special about it, it just uses type on a functions __code__.
Since it's in the standard lib, you can be sure it will work even if some change happens in the way code objects are exposed.

How to get a function's name as string?

In Python, how do I get a function's name as a string?
I want to get the name of the str.capitalize() function as a string. It appears that the function has a __name__ attribute. When I do
print str.__name__
I get this output, as expected:
str
But when I run str.capitalize().__name__ I get an error instead of getting the name "capitalize".
> Traceback (most recent call last):
> File "string_func.py", line 02, in <module>
> print str.capitalize().__name__
> TypeError: descriptor 'capitalize' of 'str' object needs an argument
Similarly,
greeting = 'hello, world'
print greeting.capitalize().__name__
gives this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__name__'
What went wrong?
greeting.capitalize is a function object, and that object has a .__name__ attribute that you can access. But greeting.capitalize() calls the function object and returns the capitalized version of the greeting string, and that string object doesn't have a .__name__ attribute. (But even if it did have a .__name__, it'd be the name of the string, not the name of the function used to create the string). And you can't do str.capitalize() because when you call the "raw" str.capitalize function you need to pass it a string argument that it can capitalize.
So you need to do
print str.capitalize.__name__
or
print greeting.capitalize.__name__
Let's start from the error
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'name'
Specific
AttributeError: 'str' object has no attribute 'name'
You are trying
greeting = 'hello, world'
print greeting.capitalize().__name__
Which will capitalize hello world and return it as a string.
As the error states, string don't have attribute _name_
capitalize() will execute the function immediately and use the result whereas capitalize will represent the function.
If you want to see a workaround in JavaScript,
Check the below snippet
function abc(){
return "hello world";
}
console.log(typeof abc); //function
console.log(typeof abc());
So, don't execute.
Simply use
greeting = 'hello, world'
print greeting.capitalize.__name__
You don't need to call this function and simply use name
>>> str.capitalize.__name__

implementing a deferred exception in Python

I would like to implement a deferred exception in Python that is OK to store somewhere but as soon as it is used in any way, it raises the exception that was deferred. Something like this:
# this doesn't work but it's a start
class DeferredException(object):
def __init__(self, exc):
self.exc = exc
def __getattr__(self, key):
raise self.exc
# example:
mydict = {'foo': 3}
try:
myval = obtain_some_number()
except Exception as e:
myval = DeferredException(e)
mydict['myval'] = myval
def plus_two(x):
print x+2
# later on...
plus_two(mydict['foo']) # prints 5
we_dont_use_this_val = mydict['myval'] # Always ok to store this value if not used
plus_two(mydict['myval']) # If obtain_some_number() failed earlier,
# re-raises the exception, otherwise prints the value + 2.
The use case is that I want to write code to analyze some values from incoming data; if this code fails but the results are never used, I want it to fail quietly; if it fails but the results are used later, then I'd like the failure to propagate.
Any suggestions on how to do this? If I use my DeferredException class I get this result:
>>> ke = KeyError('something')
>>> de = DeferredException(ke)
>>> de.bang # yay, this works
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __getattr__
KeyError: 'something'
>>> de+2 # boo, this doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'DeferredException' and 'int'
Read section 3.4.12 of the docs, "Special method lookup for new-style classes." It explains exactly the problem you have encountered. The normal attribute lookup is bypassed by the interpreter for certain operators, such as addition (as you found out the hard way). Thus the statement de+2 in your code never calls your getattr function.
The only solution, according to that section, is to insure that "the special method must be set on the class object itself in order to be consistently invoked by the interpreter."
Perhaps you'd be better off storing all your deferred exceptions in a global list, wrapping your entire program in a try:finally: statement, and printing out the whole list in the finally block.

Is it possible to mock the string module from Python?

For instance, if I have a call to the split method (i.e. some_string.split(":") )
Is is possible to mock this. I wanted to assert that the split function is called using assert_called_once_with
I confirm you can't do that because split() is a built-in attribute of str object and you can't set attributes of built-in or extension because they are readonly.
Below some inconclusive tests after trying into a Python 2.7.10 interpreter
>>> __builtins__.str.split
<method 'split' of 'str' objects>
>>> type(__builtins__.str.split)
<type 'method_descriptor'>
Trying to override it using a function
>>> type(lambda f:f)
<type 'function'>
>>> __builtins__.str.split = lambda f: f
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'str'
Trying to override it using a callable (function or method)
>>> type(callable)
<type 'builtin_function_or_method'>
>>> __builtins__.str.split = callable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'str'
After having a look more deeply into the CPython source code here [1]. It's a limitation in Objects/typeobject.c introduce by the function list below. This function check if we try to set a readonly attribute and raise TypeError.
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(
PyExc_TypeError,
"can't set attributes of built-in/extension type '%s'",
type->tp_name);
return -1;
}
if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
return -1;
return update_slot(type, name);
}
[1] https://hg.python.org/cpython/file/tip/Objects/typeobject.c#l3022
Yes it is with a couple of caviats.
In my case I have successfully mocked str in python3 so I can assert that split is being called with a specific input
There are two caviats
With patch, I replaced the original str class with a new class that inherits from str
In the code that I was testing, I had to do a redundant string casting like str(str_val).split
Here's how one can do it:
class MyStr(str):
def split(self, sep=None, maxsplit=-1)):
expected_str = "some_input_mutated_inside_fn_before_split_called"
self.assertEqual(self, expected_str)
return super().split(sep=sep, maxsplit=maxsplit)
with patch('mymodule.str', new=MyStr):
output = mymodule.function_that_calls_string_split(
"some_input"
)

Function Annotations giving error in python?

So I made a function, and wanted to add annotations to it, and the compiler keeps giving me an error:
def square_root(x:number, eps:number) -> float:
pass
And the compiler returns this:
File "/Users/albertcalzaretto/Google Drive/CSC148H1/e1/e1a.py", line 1
def square_root(x, eps) -> float:
^
SyntaxError: invalid syntax
I've never used function annotations, and I've read several sources about it, and I don't think what I'm doing is wrong.
Two things:
You must be using Python 2.x somehow. Function annotations are only supported in Python 3.x. If you try to use them in Python 2.x, you will get a SyntaxError:
>>> def f() -> int:
File "<stdin>", line 1
def f() -> int:
^
SyntaxError: invalid syntax
>>>
If number is undefined (which I believe it is), then you need to make it a string so that you don't get a NameError. Below is a demonstration:
>>> def square_root(x:number, eps:number) -> float:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'number' is not defined
>>>
>>> def square_root(x:'number', eps:'number') -> float:
... pass
...
>>>

Categories