require function for parameter python [duplicate] - python

This question already has answers here:
How can I specify the function type in my type hints?
(5 answers)
Closed 1 year ago.
So I decided I wanted to make my own custom event system thing and it all works except that on line 2 python is like:
Traceback (most recent call last):
File "d:\CollidaCube\VSCode\GameAPY\event.py", line 1, in <module>
class EventListener():
File "d:\CollidaCube\VSCode\GameAPY\event.py", line 2, in EventListener
def __init__(self, func: function):
NameError: name 'function' is not defined
why is this and how can i fix it? my code

Seon has responded with a valid answer to my question. This has been answered here. In my case, I have decided to use type(abs). its simple and I understand it.
Edit:
After some experimentation I discovered that my VSCode nor python will yell at me if I do type[abs].

Related

How can I open the underlying script of built-in functions? [duplicate]

This question already has answers here:
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
How can I open the underlying script for built-in functions? For example the function len.
I want to open the script of the len function to study how the underlying code is built, for educational purposes.
I have tried writing open(len) and open(len()), but I only get this error message:
Traceback (most recent call last):
File "C:/Users/someo/PycharmProjects/Test/test.py", line 1, in <module>
open(len())
TypeError: len() takes exactly one argument (0 given)
These definitions can't be accessed, because they aren't part of a package. They live in Python itself, therefore they cannot be accessed. You can't get in by making them raise an error either (len(5) won't let you see inside the function, and will just raise a TypeError). Python itself won't show you the full traceback because these definitions aren't meant to be seen.

weird * in function definition [duplicate]

This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
Closed 4 years ago.
I met a weird case in the function definition in python, I read some code like this:
def abc(dd, *, ee=None):
print(dd, ee)
In the beginning, I thought this code is wrong and maybe a typo of *args, but recently I tried this code in latest python3.7, and it seems that it can be interpreted, and usage is also super wired, you can't pass more than 1 argument to this function:
>>> abc(11, 222)
Traceback (most recent call last):
File "<input>", line 1, in <module>
abc(11, 222)
TypeError: abc() takes 1 positional argument but 2 were given
>>> abc(11)
11 None
I am asking because I don't know the purpose of why some one wrote like this, and why python support this behaviour in Python3(not supported in python2)
It seems your function has 1 positional argument and one named argument. The * eats all other positional arguments, just like an *args would, but you cannot reference them.

NameError: name 'xrange' is not defined when running tensorflow object detection api [duplicate]

This question already has answers here:
NameError: global name 'xrange' is not defined in Python 3
(6 answers)
Closed 4 years ago.
I run python object_detection/builders/model_builder_test.py with Tensorflow 1.9 of CPU version and Python 3.6 under Ubuntu system, there is NameError: name 'xrange' is not defined, does someone know why this happens and how to deal with it? Thanks. Here is the guide i follow.
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md
Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 311, in test_create_ssd_resnet_v1_fpn_model_from_config
model = model_builder.build(model_proto, is_training=True)
File "/home/tensorflow/models/research/object_detection/builders/model_builder.py", line 110, in build
add_background_class)
File "/home/tensorflow/models/research/object_detection/builders/model_builder.py", line 214, in _build_ssd_model
ssd_config.anchor_generator)
File "/home/tensorflow/models/research/object_detection/builders/anchor_generator_builder.py", line 91, in build
cfg.normalize_coordinates
File "/home/tensorflow/models/research/object_detection/anchor_generators/multiscale_grid_anchor_generator.py", line 61, in __init__
for scale in xrange(scales_per_octave)]
NameError: name 'xrange' is not defined
xrange() was removed from python3 and was replaced by range(). Use range() instead (it works exactly the same as xrange()).
However, if you need to use the range() function in python2 (which you don't in this case), use xrange() as range() returns a list instead of a generator (as #xdurch0 says in the comments).

`AttributeError` while trying to access 2d list within the same class (python) [duplicate]

This question already has answers here:
Python Variable Declaration
(6 answers)
Closed 3 years ago.
the code of my program is here: Why am I getting this error "NameError:name 'self' is not defined."
The error I get is the following:
Traceback (most recent call last):
File "sudoku_maker.py", line 51, in <module>
p.main()
File "sudoku_maker.py", line 44, in main
self.createEasy()
File "sudoku_maker.py", line 16, in createEasy
if (self.puzzle[i][j] != 0):
AttributeError: 'Puzzle' object has no attribute 'puzzle'
The only reason I could see why this error is happening is because the list is only declared in the init function but I put it in there because I saw another answer on here that said to do it that way. I was gonna comment on the answer saying asking how to do that for class variable but I didn't have enough rep and then I found another question where the answerer said to only declare a list in the init function.
Why haven't you initialised puzzle as an instance variable? As it is, it is only a local variable. You'll need:
def __init__(self, **puzzle):
self.puzzle = [[0 for x in range(9)]for y in range(9)]
self is your instance, and self.x = ... will result in x, an instance variable, and is accessible as an attribute of self.

Weirdness calling str() to convert integer to string in Python 3? [duplicate]

This question already has answers here:
Why does code like `str = str(...)` cause a TypeError, but only the second time?
(20 answers)
Closed last month.
Why is this giving me an error?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
That code alone won't give you an error. For example, I just tried this:
~ $ python3.2
>>> variable = str(21)
>>> variable
'21'
Somewhere in your code you're defining that str = something else, masking the builtin definition of str. Remove that and your code will work fine.
Because you've probably overwritten the str function by calling your own variable str.

Categories