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
This is my code
class Dog():
def _init_(self,breed):
self.breed = breed
And when I run
my_dog = Dog(breed = "Lab")
I get the following error
Dog() takes no arguments
If I run my_dog = Dog()
then there is no error!!
You define a constructor the wrong way
def __init__ (self, breed, name):
self.breed = breed
Related
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())
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 trying to find a way to store class attribute information in a list when the object is created. I am just learning python classes and am having some difficulty figuring out how to implement the str method correctly.
muffins = []
class Muffin:
def __init__(self,name,calories):
self.name = name
self.calories = calories
muffins.append(Muffin)
def __str__(self):
return self.name
banana_chocolate_chip = Muffin('banana_chocolate_chip', 250)
blueberry = Muffin('blueberry', 200)
pumpkin_pecan = Muffin('pumpkin_pecan', 225)
for muffin in muffins:
print(muffin)
When I run this code I get:
<class '__main__.Muffin'>
<class '__main__.Muffin'>
<class '__main__.Muffin'>
My desired output is:
banana_chocolate_chip
blueberry
pumpkin_pecan
Thanks in advance! :)
Use muffins.append(self) instead of muffins.append(Muffin) to get your desired output. muffins.append(Muffin) doesn't append an actual instance of the muffin class, passing self will refer to the instance that just got created.
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
class Person():
def insert(self,name,age,idName):
self.name=name
self.age=age
self.idName=idName
def output(self):
print('name='+name+'\nage='+age+'\nidName='+idName)
j=Person()
j.insert('Alex','40','3143450603')
j.output
Nothing outputted in a terminal, maybe some syntax error
First of all in output method you have to access the variables with self..
Secondly, you are caaling output method without braces which needs to be fixed.
Try this :
class Person():
def insert(self,name,age,idName):
self.name=name
self.age=age
self.idName=idName
def output(self):
print('name='+self.name+'\nage='+self.age+'\nidName='+self.idName)
j=Person()
j.insert('Alex','40','3143450603')
j.output()
This is the output you will get :
name=Alex
age=40
idName=3143450603
In the method Person.output() you are referring to the local variables name, age and idName, whereas you should be referring to the object's members (self.name, ...), because the local variables do not exist at this point. They only existed in Person.insert().
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 6 years ago.
Improve this question
class Person(object):
def __init__(self, age):
self.age = age
self.ageGroup = ageGroup
def findAgeGroup(self):
if age >= 80:
ageGroup= "old"
print ageGroup
John= Person(95)
John.findAgeGroup
So my Question is probably a pretty simple one. In the above code in the __init__ method the variable self.age is set when a new instance of the Person class is instantiated? And all of the other variables in the __init__ method are put there because they are related to self.age? For example in the findAgeGroup method age is used to derive a value for ageGroup. So would the only time you list self.ageGroup in the __init__ method be if you plan to derive the value from self.age which is called in when a new instance of the class is created?
In Python you actually always need the self to reference instance variables unlike e.g. Java's this:
def findAgeGroup(self):
if self.age >= 80:
self.ageGroup = "old"
print self.ageGroup
# also Python prefers snake_case: self.age_group
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 am developing a new class and to find the length won't work. Here is the code, and suggestions:
class Queue:
def __init__(self):
self.queue = []
self.out_stack = []
def enqueue(self, other='string'):
self.queue.append(other)
def __len__(self):
len(self.queue)
It should be return len(self.queue), not just len(self.queue).
You actually have to return the len-value:
def __len__(self):
return len(self.queue)
Otherwise it will just be calculated and then nothing happens