I have a base class Person
and derived classes Manager
and Employee
. Now, what I would like to know is the object created is Manager
or the Employee
.
我有一个基类Person和派生类Manager和Employee。现在,我想知道的是创建的对象是Manager还是Employee。
The person is given as belows:
该人的具体如下:
from Project.CMFCore.utils import getToolByName
schema = getattr(Person, 'schema', Schema(())).copy() + Schema((TextField('FirstName', required = True, widget = StringWidget(label='First Name', i18n_domain='project')), TextField('Last Name', required = True, widget = StringWidget(label='Last Name', i18n_domain='i5', label_msgid='label_pub_city'))
class Manager(BaseContent):
def get_name(self):
catalog = getToolByName(self, "portal_catalog")
people = catalog(portal_type='Person')
person={}
for object in people:
fname = object.firstName
lname = object.lastName
person['name'] = fname+' '+ lname
# if the derived class is Employee then i would like go to the method title of employee and if its a Manager then go to the title method of Manager
person['post'] = Employee/Manager.title()
return person
For Manager and employees they are like (employee is also similar but some different methods)
对于经理和员工而言他们就像(员工也很相似,但有些不同的方法)
from Project.Person import Person
class Manager(Person):
def title(self):
return "Manager"
For Employee the title is 'Employee'. When I create a Person
it is either Manager
or the Employee
. When I get the person object the class is Person but I would like to know whether it is from the derived class 'Manager' or 'Employee'.
对于员工,标题是“员工”。当我创建Person时,它是Manager或Employee。当我获得person对象时,该类是Person,但我想知道它是来自派生类'Manager'还是'Employee'。
6 个解决方案
#1
29
I don't know if this is what you want, and the way you'd like it implemented, but here's a try:
我不知道这是不是你想要的,以及你希望它实现的方式,但这是一个尝试:
>>> class Person(object):
def _type(self):
return self.__class__.__name__
>>> p = Person()
>>> p._type()
'Person'
>>> class Manager(Person):
pass
>>> m = Manager()
>>> m._type()
'Manager'
>>>
Pros: only one definition of the _type
method.
优点:只有一个_type方法的定义。
#2
7
Python objects provide a __class__
attribute which stores the type used to make that object. This in turns provides a __name__
attribute which can be used to get the name of the type as a string. So, in the simple case:
Python对象提供__class__属性,该属性存储用于生成该对象的类型。这反过来提供了__name__属性,可用于将类型的名称作为字符串获取。所以,在简单的情况下:
class A(object):
pass
class B(A):
pass
b = B()
print b.__class__.__name__
Would give:
'B'
So, if I follow your question correctly you would do:
所以,如果我正确地按照你的问题你会做:
m = Manager()
print m.__class__.__name__
'Manager'
#3
5
Not exactly sure that I understand what you are asking, but you can use x.__class__.__name__
to retrieve the class name as a string, e.g.
不完全确定我理解你在问什么,但是你可以使用x .__ class __.__ name__来检索类名作为字符串,例如
class Person:
pass
class Manager(Person):
pass
class Employee(Person):
pass
def get_class_name(instance):
return instance.__class__.__name__
>>> m = Manager()
>>> print get_class_name(m)
Manager
>>> print get_class_name(Employee())
Employee
Or, you could use isinstance to check for different types:
或者,您可以使用isinstance检查不同类型:
>>> print isinstance(m, Person)
True
>>> print isinstance(m, Manager)
True
>>> print isinstance(m, Employee)
False
So you could do something like this:
所以你可以这样做:
def handle_person(person):
if isinstance(person, Manager):
person.read_paper() # method of Manager class only
elif isinstance(person, Employee):
person.work_hard() # method of Employee class only
elif isinstance(person, Person):
person.blah() # method of the base class
else:
print "Not a person"
#4
5
Would you be looking for something like this?
你会找这样的东西吗?
>>> class Employee:
... pass
...
>>> class Manager(Employee):
... pass
...
>>> e = Employee()
>>> m = Manager()
>>> print e.__class__.__name__
Employee
>>> print m.__class__.__name__
Manager
>>> e.__class__.__name__ == 'Manager'
False
>>> e.__class__.__name__ == 'Employee'
True
#5
4
The best way to "do this" is to not do it. Instead, create methods on Person that are overridden on Manager or Employee, or give the subclasses their own methods that extend the base class.
“做到这一点”的最好方法是不要这样做。相反,在Person上创建在Manager或Employee上重写的方法,或者为子类提供扩展基类的自己的方法。
class Person(object):
def doYourStuff(self):
print "I'm just a person, I don't have anything to do"
class Manager(object):
def doYourStuff(self):
print "I hereby manage you"
class Employee(object):
def doYourStuff(self):
print "I work long hours"
If you find yourself needing to know in the base class which subclass is being instantiated, your program probably has a design error. What will you do if someone else later extends Person to add a new subclass called Contractor? What will Person do when the subclass isn't any of the hard-coded alternatives it knows about?
如果您发现自己需要在基类中知道正在实例化哪个子类,那么您的程序可能存在设计错误。如果其他人后来扩展Person以添加一个名为Contractor的新子类,你会怎么做?当子类不是它知道的任何硬编码替代品时,Person会做什么?
#6
0
In your example you do not need to know the class, you just call the method by referring to the class instance:
在您的示例中,您不需要知道该类,只需通过引用类实例来调用该方法:
# if the derived class is Employee then i would like go to the method title
# of employee and if its a Manager then go to the title method of Manager
person['post'] = object.title()
But do not use object
as a variable name, you hide the built-in name.
但是不要使用object作为变量名,隐藏内置名称。
#1
29
I don't know if this is what you want, and the way you'd like it implemented, but here's a try:
我不知道这是不是你想要的,以及你希望它实现的方式,但这是一个尝试:
>>> class Person(object):
def _type(self):
return self.__class__.__name__
>>> p = Person()
>>> p._type()
'Person'
>>> class Manager(Person):
pass
>>> m = Manager()
>>> m._type()
'Manager'
>>>
Pros: only one definition of the _type
method.
优点:只有一个_type方法的定义。
#2
7
Python objects provide a __class__
attribute which stores the type used to make that object. This in turns provides a __name__
attribute which can be used to get the name of the type as a string. So, in the simple case:
Python对象提供__class__属性,该属性存储用于生成该对象的类型。这反过来提供了__name__属性,可用于将类型的名称作为字符串获取。所以,在简单的情况下:
class A(object):
pass
class B(A):
pass
b = B()
print b.__class__.__name__
Would give:
'B'
So, if I follow your question correctly you would do:
所以,如果我正确地按照你的问题你会做:
m = Manager()
print m.__class__.__name__
'Manager'
#3
5
Not exactly sure that I understand what you are asking, but you can use x.__class__.__name__
to retrieve the class name as a string, e.g.
不完全确定我理解你在问什么,但是你可以使用x .__ class __.__ name__来检索类名作为字符串,例如
class Person:
pass
class Manager(Person):
pass
class Employee(Person):
pass
def get_class_name(instance):
return instance.__class__.__name__
>>> m = Manager()
>>> print get_class_name(m)
Manager
>>> print get_class_name(Employee())
Employee
Or, you could use isinstance to check for different types:
或者,您可以使用isinstance检查不同类型:
>>> print isinstance(m, Person)
True
>>> print isinstance(m, Manager)
True
>>> print isinstance(m, Employee)
False
So you could do something like this:
所以你可以这样做:
def handle_person(person):
if isinstance(person, Manager):
person.read_paper() # method of Manager class only
elif isinstance(person, Employee):
person.work_hard() # method of Employee class only
elif isinstance(person, Person):
person.blah() # method of the base class
else:
print "Not a person"
#4
5
Would you be looking for something like this?
你会找这样的东西吗?
>>> class Employee:
... pass
...
>>> class Manager(Employee):
... pass
...
>>> e = Employee()
>>> m = Manager()
>>> print e.__class__.__name__
Employee
>>> print m.__class__.__name__
Manager
>>> e.__class__.__name__ == 'Manager'
False
>>> e.__class__.__name__ == 'Employee'
True
#5
4
The best way to "do this" is to not do it. Instead, create methods on Person that are overridden on Manager or Employee, or give the subclasses their own methods that extend the base class.
“做到这一点”的最好方法是不要这样做。相反,在Person上创建在Manager或Employee上重写的方法,或者为子类提供扩展基类的自己的方法。
class Person(object):
def doYourStuff(self):
print "I'm just a person, I don't have anything to do"
class Manager(object):
def doYourStuff(self):
print "I hereby manage you"
class Employee(object):
def doYourStuff(self):
print "I work long hours"
If you find yourself needing to know in the base class which subclass is being instantiated, your program probably has a design error. What will you do if someone else later extends Person to add a new subclass called Contractor? What will Person do when the subclass isn't any of the hard-coded alternatives it knows about?
如果您发现自己需要在基类中知道正在实例化哪个子类,那么您的程序可能存在设计错误。如果其他人后来扩展Person以添加一个名为Contractor的新子类,你会怎么做?当子类不是它知道的任何硬编码替代品时,Person会做什么?
#6
0
In your example you do not need to know the class, you just call the method by referring to the class instance:
在您的示例中,您不需要知道该类,只需通过引用类实例来调用该方法:
# if the derived class is Employee then i would like go to the method title
# of employee and if its a Manager then go to the title method of Manager
person['post'] = object.title()
But do not use object
as a variable name, you hide the built-in name.
但是不要使用object作为变量名,隐藏内置名称。