Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to add the age for users in a different method but some users might not have an age argument
class User:
"""a class to save info about every user """
def __init__(self, user_name, num_id):
self.name = name
self.mun_id = num_id
def age(self, age):
self.age = age
user1 = User("martin", "1")
print (user1.name)
Yes you can set user age separately. Example below:
user1.age(20)
print (user1.age)
#20 will print
define age = None like below and this optional argument should be the last one:
def age(self, age = None):
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
I am trying to create an object and I'm initializing it with 2 fields first (name & number). However each time I run the program I'm given this error:
TypeError: __init__() missing 1 required positional argument: 'number'
The __init__ method in the class looks like this:
def __init__(self, name, number):
self.__name__ = name
self.__number__ = number
The code where I try to create the object is this:
employee1 = ProductionWorker(Employee)
name = input("Enter employee name:")
number = input("Enter employee number:")
employee1.__init__(name, number)
Does anyone know why I may be getting this error?
Do:
name = input("Enter employee name:")
number = input("Enter employee number:")
employee1 = ProductionWorker(name, number)
You do not generally need to call __init__ explicitly; it's invoked by the ProductionWorker(...) expression, which passes its arguments to self.__init__ as part of initialization.
You do not need to restate when constructing a new object that Employee is the parent class; that only needs to be said when the class is defined.
You are using the class as a parameter.
Then, init is always automatically called on creation. This how it might work as You want (using the show method to validate):
class Employee:
def __init__(self, name, number):
self.__name__ = name
self.__number__ = number
def show(self):
print(self.__name__)
print(self.__number__)
name = input("Enter employee name:")
number = input("Enter employee number:")
employee1 = Employee(name,number)
employee1.show()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to create new class by type function.
def create_fk_widget_from_model(model, **kwargs):
to_field = kwargs.pop('to_field', 'id')
rel = ManyToOneRel(None, model, to_field) # type: ignore
return type(
f'{model.__name__}ForeignKeyRawIdWidget',
(ForeignKeyRawIdWidget, ),
{'__init__': ForeignKeyRawIdWidget.__init__(
rel=rel,
admin_site=admin.site)}
)
But there is a problem. I need to change __init__ in new class, how to do it?
You probably want something like:
def create_fk_widget_from_model(model, **kwargs):
to_field = kwargs.pop('to_field', 'id')
rel = ManyToOneRel(None, model, to_field) # type: ignore
def __init__(self):
return ForeignKeyRawIdWidget.__init__(self, rel=rel, admin_site=admin.site)
return type(
f'{model.__name__}ForeignKeyRawIdWidget',
(ForeignKeyRawIdWidget, ),
{'__init__': __init__}
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Hi I am new to Python and I am trying to make a class out of a list from a file and I am wondering if it is even possible or is it better to use dictionaries. My class looks like this:
class Program:
def __init__(self, name, start_time, end_time, channel):
And the list looks like it:
['Channel 1', '16.00-17.45 News', '17.45-17.50 Weather', '17.50-17.57 Friends', '17.57-18.00 Coming up', '18.00-18.15 MASH', '18.15-18.40 Thundercats]
Is there ant easy way to do it?
This is one of the approach:
data = ['Channel 1', '16.00-17.45 News', '17.45-17.50 Weather', '17.50-17.57 Friends', '17.57-18.00 Coming up', '18.00-18.15 MASH', '18.15-18.40 Thundercats']
class Program:
def __init__(self, start_time, end_time, name):
self.start_time = start_time
self.end_time = end_time
self.name = name
class Channel:
def __init__(self, channel_name, program_list):
self.channel_name = channel_name
self.program_list = program_list
plist = []
channel1 = Channel(data[0], plist)
for i in range(1, len(data)):
name = data[i][12:]
start_time = data[i][0:5]
end_time = data[i][7:12]
p = Program(start_time, end_time, name)
channel1.program_list.append(p)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm studying Python programming and I'm having difficulty understanding Inheritance. My assignment is to:
Create a Division and Department class.
Create a method named “getList()” which will display a message, “The
dept department has fullTime full-time and partTime part-time
instructors.”
In the “Department” class, assign 12 as value to the fullTime
variable, assign 27 to partTime, and “CIS” to dept. DO NOT create
any method in the “Department” class. and
Create an instance (object) of the “Department” class named
“myDept”. Use this “myDept” object to call the “getList()” method of
“Division” class (through Inheritance).
Here's what I have so far.
class Division():
def __init__(self, dept, fullTime, partTime):
self.dept = dept
self.fullTime = fullTime
self.partTime = partTime
def getList(self):
return "The (0) department has (1) full-time and (2) part-time instructors.".format(self.dept, self.fullTime, self.partTime)
class Department(Division):
myDept = Division(CIS247, 12, 27)
class Division(object):
def __init__(self,dept, fullTime, partTime):
self.fullTime = fullTime
self.partTime = partTime
self.dept=dept
def getList(self):
return "The {0} department has {1} full-time and {2} part-time instructors.".format(self.dept, self.fullTime, self.partTime)
class Department(Division):
pass
myDept = Department("CIS",12,37)
print myDept.getList()
Edited, I missed the "CIS", for string formatting you use {} not ().
Also " DO NOT create any method in the “Department” class." so removed init method.
python classes tutorial
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to call/run a method only onetime I tried this but it didn't wotk:
class S ()
_int_(self)
self.xxx = True # i tried with and without
def Packet (event):
if (xxx == True):
self.f(event, xxx)
print xxx
else:
....
def f (event):
print "something"
Do_Somthing
xxx=False
the problem xxx is still true
Best regards
Amer
The whole class's syntax seems wrong to me. You can do something like this
class S:
def __init__(self): # Initializer function for instance members
self.flag = True
def myMethod(self): # Actual method to be called
if self.flag:
....
....
self.flag = False
Change xxx to self.xxx.
The xxx = False creates a new name binding instead of assigning to the field in your object.
Also, there are also some other syntax errors in your code. Is this the actual code you are running? The code you posted shouldn't run.
from itertools import count
class S ()
def __init__(self)
self.xxx = count()
def Packet(self, event):
if next(self.xxx) == 0:
self.f(event)
else:
....
def f(self, event):
print "something"
#Do_Something