Variable scope in __init__ method? [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 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

Related

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

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