Creating a len method in a new class - python [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 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

Related

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())

How to store class attribute information in a list when creating an object? [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 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.

Not able to create a object with attributes [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
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

Return only the property of enum, not Class.Property [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 have the following code:
from enum import Enum
class BetterChoices(Enum):
#classmethod
def choices(cls):
return [(tag.name, tag.value) for tag in cls]
class SensorStatus(BetterChoices):
UNASSIGNED = 'Sin asignar'
ASSIGNED = 'Asignado'
If I do print(SensorStatus.ASSIGNED.name) it returns SensorStatus.ASSIGNED. If I do print(SensorStatus.ASSIGNED.value) it returns 'Sin asignar'. What if I just want to return the name, not the Class.name?
So, if I do print(x) it will return ASSIGNED as a string.
You can do that by implementing the __str__ magic method in your BetterChoices class:
def __str__(self):
return self.name
Each python object has string representation. When you do str(object) or print(object) Python implicitly calls object.__str__. Enum's default string representation has form <Classname>.<Attribute>. To override that you just need implement your own version of __str__.

Working with classes in Python 3.8.2, Didn't get any output [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
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().

Categories

Resources