I'm following the book "Python crash Course" and i keep getting this
NameError: name 'get_step' is not defined
It's in python3 and both files are in the same directory.
So..the function is defined inside this class:
and the class is imported to:
where it calls fill_walks, and fill_walks calls get_step. What am i missing here? can someone help me please
You know how when you refer to x_values, y_values, and num_points, you have to say self.x_values, self.y_values, etc? This is true for all attributes of your instance, including methods. So when you want to do get_step() and get_step is defined on your class, you have to call self.get_step().
In addition, when you define a method on your class, by default it's what's called an instance method, and will automatically be called with the instance as the first argument. You've defined fill_walk correctly, with def fill_walk(self): but get_step is currently not defined properly. You either have to do
def get_step(self):
....
or, since get_step doesn't itself need to access any instance attributes, you can mark it as a static method by defining it like so:
#staticmethod
def get_step():
...
in random_walk.py
change
def get_step():
to
def get_step(self): #<--- self?
Related
for example:
for code like this,
class Solution:
def __init__(self, N, weights):
can i write like this
class Solution:
def snowball(self, N, weights):
if not then why?
No this is a magic method of class, so you can't overwrite it. The snowball method you call is not a magic, and you can't "declare" it as a magic method. The snowball method is just a normal method of class's. You can't initialize a new instance with it.
For more (https://www.tutorialsteacher.com/python/magic-methods-in-python)
I don't understand why would you like to change the constructor method (init).
The init method lets the class initialize the object's attributes and serves no other purpose. Aditionally, as all the functions that starts with double underscore, the constructor method it's a special method and it's only purpose it's to construct the class.
If you are willing to change that name, you could rename it like:
__init__ = __newconstructorname__
You can check more info here:
https://www.udacity.com/blog/2021/11/__init__-in-python-an-overview.html
My attempt was to create the default instance from inside of a metaclass, but to no avail. At least the reported class is the singleton in the example bellow.
EDIT: Clarifying requirements here: a singleton comparable by using the is keyword, without having to instantiate/call it. Unfortunately, this well known question-answer here doesn't seem to address that.
class MyNormalClass:
def __init__(self, values):
self.values = values
class MySingleton(MyNormalClass, type):
def __new__(mcs, *args, **kwargs):
return MyNormalClass(["default"])
print(MySingleton)
# <class '__main__.MySingleton'>
print(MySingleton.values)
# AttributeError: type object 'MySingleton' has no attribute 'values'
Metaclasses for singletons are overkill. (Search my answers for that, and there should be about 10 occurrences of this phrase).
In your example code in the question, the code inside the class and the metaclass methods is not even being run, not once. There is no black-magic in Python - the program just runs, and there are a few special methods marked with __xx__ that will be called intrinsically by the language runtime. In this case, the metaclass __new__ will be called whenever you create a new class using it as the metaclass, which you code does not show. And also, it would create a new instance of your "singleton" class each time it were used.
In this case, if all you need is a single instance, just create that instance, and let that one be public, instead of its class. You can even us the instance itself to shadow the class from the module namespace, so no one can instantiate it again by accident. If you want values to be immutable, well, you can't
ensure that with pure Python code in any way, but you can make changing values do not work casually with = so that people will know they should not be changing it:
class MySingleton:
__slots__ = ("values",)
def __init__(self, values):
self.values = values
def lock(self, name, value):
raise TypeError("Singleton can't change value")
self.__class__.__setitem__ = lock
MySingleton = MySingleton(["values"])
So, I might just be getting the basics confused here. But, it is printing even if I don't call the class.
I have tried putting it in a function, but only when I run the function there are problems.
class something_im_doing_wrong():
print('this should not be printing unless I call the class')
def __init__(self):
self.info = 'weird'
Now, the output is
this should not be printing unless I call the class
Even though I haven't called the class it is printing
I expect the output to be nothing, yet is is still printing. I want it so it only prints when I call the class.
If you want it to be printed when the class instance is created, put it inside the function __init__.
This is the initializer method which is automatically called whenever a new instance of a class is created.
If you want your string to get printed when you create a class, put the print statement in this function. This function gives the programmer the opportunity to set up the attributes required within the new instance by giving them their initial state/values.
I learned __new__ from new and init | Spyhce blog such an example:
class A(object):
def __new__(cls):
return super(A, cls).__new__(cls) #I think here is an infinite recursive
the code could be rewritten as
class A(object):
def __new__(A):
return object.__new__(A)
It's algorithms:
1, define A inherit from object
2, override method __new__ by def __new__(A) but parameter A is not implemented until it is called
3, object.new(A), recursively call A
This is definitely an infinite recursively iterating.
How the infinite loop stop?
This doesn't cause an infinite recursion because you aren't calling the __new__ method of the A() class. You are calling the __new__() method of it's super class which is still untouched and behaves as default.
So It's simple now, as the method belongs to its superclass not your A class, so this
__new__ requires class reference to be passed the first argument and it will create the instance for you. And that's what this super(A, cls).__new__(cls) line does.
As is clearly said in the comments of the other answer, you are mistaking the variable A local to the staticmethod __new__ for the class name A, which exists in a more global scope. By convention, this is something like cls for __new__, and your choice of typing A is confusing you. I think this is an example of massive overthinking of one of the first-learned concepts in Python.
That A in the inner call to object.__new__ is not the same variable as the A you chose for the class name. Scope is relevant no matter if programming magic methods or simple scripts.
You erroneously changed this between examples.... leading to your confusion. cls (as you wrote correct in first version) is essentially a placeholder for the class definition, which once and here only is sent to a different method so without a call back, like if object.__new__(x) called x.__new__, how could there be recursion?
I'm having trouble getting my head around assigning a function to a variable when the function uses arguments. The arguments appear to be required but no matter what arguments I enter it doesn't work.
The scenario is that I'm creating my first GUI which has been designed in QT Designer. I need the checkbox to be ticked before the accept button allows the user to continue.
Currently this is coded to let me know if ticking the checkbox returns anything (which is does) however I don't know how to pass that result onto the next function 'accept_btn'. I thought the easiest way would be to create a variable however it requires positional arguments and that's where I'm stuck.
My code:
class MainWindow(QtWidgets.QMainWindow, Deleter_Main.Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.ConfirmBox.stateChanged.connect(self.confirm_box)
self.Acceptbtn.clicked.connect(self.accept_btn)
def confirm_box(self, state):
if self.ConfirmBox.isChecked():
print("checked")
else:
print("not checked")
checked2 = confirm_box(self, state)
def accept_btn(self):
if checked2 == True:
print("clicked")
else:
print("not clicked")
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
The code gets stuck on 'checked2' with the error:
NameError: name 'self' is not defined
I thought there might be other solutions for running this all within one function but I can't seem to find a way whilst the below is required.
self.ConfirmBox.stateChanged.connect(self.confirm_box)
Would extra appreciate if anyone could help me understand exactly why I need the 'self' argument in the function and variable.
Thanks in advance,
If you just need to enable a button when the checkbox is checked, it can be easily done within the signal connection:
self.ConfirmBox.toggled.connect(self.Acceptbtn.setEnabled)
QWidget.setEnabled requires a bool argument, which is the argument type passed on by the toggled signal, so the connection is very simple in this case.
Apart from this, there are some mistakes in your understanding of classes in Python: it seems like you are thinking in a "procedural" way, which doesn't work well with general PyQt implementations and common python usage, unless you really need some processing to be done when the class is created, for example to define some class attributes or manipulate the way some methods behave. But, even in this case, they will be class attributes, which will be inherited by every new instance.
The line checked2 = confirm_box(self, state) will obviously give you an error, since you are defining checked2 as a class atribute. This means that its value will be processed and assigned when the class is being created: at this point, the instance of the class does not exist yet, Python just executes the code that is not part of the methods until it reaches the end of the class definition (its primary indentation). When it reaches the checked2 line, it will try to call the confirm_box method, but the arguments "self" and "state" do not exist yet, as they have not been defined in the class attributes, hence the NameError exception.
Conceptually, what you have done is something similar to this:
class SomeObject(object):
print(something)
This wouldn't make any sense, since there is no "something" defined anywhere.
self is a python convention used for class methods: it is a keyword commonly used to refer to the instance of a class, you could actually use any valid python keyword at all.
The first argument of any class method is always the reference to the class instance, the only exceptions are classmethod and staticmethod decorators, but that's another story. When you call a method of an instanciated class, the instance object is automatically bound to the first argument of the called method: the self is the instance itself.
For example, you could create a class like this:
class SomeObject(object):
def __init__(Me):
Me.someValue = 0
def setSomeValue(Myself, value):
Myself.someValue = value
def multiplySomeValue(I, multi):
I.setSomeValue(I.someValue * multi)
return I.someValue
But that would be a bit confusing...