python error NameError name is not defined [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
class Bird :
'''A base class to define bird properties.'''
count = 0
def_init_( self , chat )
self.sound = chat
Bird.count += 1
def talk( self ) :
return self.sound
NameError: name 'def_init_' is not defined
I tried using 2 underscores on both side of the 'init'

This looks like a simple typo. You're missing the space after def - it should be:
def __init__(self, chat):

you had lots of error in your script actually
1> indentation
2> give space after def
3> colon after def init( self , chat )
follow the rules then everything will be okay....
class Bird :
'''A base class to define bird properties.'''
count = 0
def __init__( self , chat ):
self.sound = chat
Bird.count += 1
def talk( self ):
return self.sound

You need to change your _init_ function to def __init__(self, chat):.
The __ is called a dunder, which is short for 'double underscore'. We usually use this, magic methods like instantiation. So for example, [] would be called __get__.
Whenever you see a dunder, you know it has a special use.

Related

Python - init argument not used in self [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I came by a code and wanted to get more understanding on when to use such arrangement.
What would be a good scenario of not using __init__'s argument (name) in self?
class ArgumentNotUsedInSelf:
def __init__(self, name: str):
self.type = "user"
user_one = ArgumentNotUsedInSelf("Mike")
print(user_one.type)
Any explanations from architectural (and not only) point of view are most welcome!
Some of the reasons for this can be:
Historical. The class used to use the argument, but it was changed so it's no longer meaningful. The argument was kept for backward compatibility.
Class hierarchy. This may be a child class of a class that uses the argument, but the child overrides the need for it. The argument is required for compatibility with the parent.
Sort of Barmar's historical example... let's say:
ArgumentNotUsedInSelf used to do something with name, but now doesn't.
But name is still used in SubClass, and changing everything could mess up dependent programs.
class ArgumentNotUsedInSelf:
def __init__(self, name: str):
self.type = "user"
class SubClass(ArgumentNotUsedInSelf):
def __init__(self, name):
super(SubClass, self).__init__(name)
self.name = name
x = SubClass('Mike')
print(x.name, x.type)
Output:
Mike user
This would be helpful if you want to store the field "name" of the created object and re-use it later:
class ArgumentNotUsedInSelf:
def __init__(self, name: str):
self.type = "user"
self.name = name
user_one = ArgumentNotUsedInSelf("Mike")
print(user_one.type)
print(user_one.name)

Class not running - getting no results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am new to python classes and I am trying to run this code but I don't get any results:
class Restaurant:
def __init__(self, mascalzone, it_fusion):
self.mascalzone = mascalzone
self.it_fusion = it_fusion
def describe_restaurant(self):
print(f"this restaurant is Italian and is named: {self.mascalzone}")
def open_restaurant(self):
print(f"the restaurant {self.it_fusion} is open , please come in!")
# make instance below:
restaurant = Restaurant('open', 9)
# printing two attributes individually:
print(f"this:{restaurant.it_fusion}")
print(f"that:{restaurant.mascalzone}")
# calling both methods:
restaurant.describe_restaurant()
restaurant.open_restaurant()
That is not how classes work. Your first part of code should be describing the class, and then the second be actually creating an instance of the class. So either you thought that you had to define classes inside of the definition, or you forgot to indent well.
in open_restaurant(), remove creating an instance of Restaurant and place it outside of the class definition. Then put the code calling both methods also outside of the code. The rest of your code is fine.
The code:
class Restaurant:
def __init__(self, mascalzone, it_fusion):
self.mascalzone = mascalzone
self.it_fusion = it_fusion
def describe_restaurant(self):
print(f"this restaurant is Italian and is named: {self.mascalzone}")
def open_restaurant(self):
print(f"the restaurant {self.it_fusion} is open , please come in!")
restaurant = Restaurant('open', 9)
restaurant.open_restaurant()
restaurant.describe_restaurant()

TypeError with the arguments of a class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
TypeError: circle() takes no arguments
with this code:
class circle:
def _init_(self,r):
self.r=self
def diameter(self):
return self.r*2
c=circle(7.0)
print("%f"%c)
How can I fix it?
A constructor is defined by the __init__ method (with two underscores), not _init_ like you have right now. Also note that you should probably be saving the passed r, not assigning self.r = self:
class circle:
def __init__(self,r):
self.r = r
Your code has some mistakes:
Python constructor has __init__ 2 underscores.
It should be %c.diameter(). You need to call the function of the class. You cannot just pass the instantiated class
class circle:
def __init__(self,r):
self.r=r
def diameter(self):
return self.r*2
c=circle(7.0)
print("%f"%c.diameter())

TypeError when adding variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I took working example code and tried to add another variable self.species = martian under the init method. Seems you cannot do this which doesn't make sense to me.
# A Sample class with init method
class Person:
# init method or constructor
def __init__(self, name, species):
self.name = name
self.species = martian
# Sample Method
def say_hi(self, name, species):
print('Hello, my name is', self.name)
print('I am', self.species)
p = Person('Martin')
p.say_hi()
TypeError: __init__() missing 1 required positional argument: 'species'
When creating the person p you need to pass the species parameter. As well, you are not using that argument in your function.
Calling could look like this:
p = Person("Martin", "martian")
Then within the constructor you reference the argument like this:
self.species = species
this is how your code should look like.
# A Sample class with init method
class Person:
# init method or constructor
def __init__(self, name, species):
self.name = name
self.species = species
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
print('I am', self.species)
p = Person('Martin', 'martian')
p.say_hi()
I suggest you watch some other beginner's video lectures on 'init' method and 'Object oriented programming in python' before writing these type of codes otherwise it would be very hard to understand beyond this point

super raises error "must be type not Jumper" python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I begin with what I think is a new style class:
class Object(object):
def __init__(self,size):
self.size=size
Then I create a subclass of this, in what appears to be the new style as well:
class Alien(Object):
def __init__(self,damage,*arg,**kwargs):
self.damage=damage
super(self,Alien).__init__(*arg,**kwargs)
self.damage=damage
Finally I create one final subclass:
class Jumper(Alien):
def __init__(self,bounceSize,*arg,**kwargs):
self.bounceSize=bounceSize
super(self,Jumper).__init__(*arg,**kwargs)
From my basic understanding of inheritance, the class Jumper should inherit all the parameters of the Object type and the Alien type, but instead when creating the object:
myObj=Jumper(size=10,damage=10,bounceSize=50)
I am hit with the error:
Traceback (most recent call last):
File "C:/Python27/test2", line 15, in <module>
myObj=Jumper(size=10,damage=25,bounceSize=50)
File "C:/Python27/test2", line 13, in __init__
super(self,Jumper).__init__(*arg,**kwargs)
TypeError: must be type, not Jumper
When checking every class with the "type()" function I find that they are all of type "type"; which I think indicates that they are of the new class type; which seems to have been the problem with other people.
So what is causing my problem?
You have self and the class in the wrong order:
class Object(object):
def __init__(self,size):
self.size=size
class Alien(Object):
def __init__(self, damage, size, *arg, **kwargs):
super(Alien, self).__init__(size)
self.damage = damage
class Jumper(Alien):
def __init__(self, bounceSize, damage, *arg, **kwargs):
super(Jumper, self).__init__(damage, *arg, **kwargs)
self.bounceSize = bounceSize

Categories

Resources